Query
1 year ago
BlockBase.php
1 year ago
BlockContainerBase.php
1 year ago
BlockElement.php
3 years ago
BlockStyle.php
4 years ago
DataHelper.php
1 year ago
TemplatePartBlockBase.php
1 year ago
BlockElement.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Element; |
| 6 | use Kubio\Core\LodashBasic; |
| 7 | use Kubio\Core\Registry; |
| 8 | use function str_replace; |
| 9 | |
| 10 | class BlockElement extends Element { |
| 11 | |
| 12 | public $config; |
| 13 | public $name; |
| 14 | |
| 15 | function __construct( $type, $props = array(), $children = array(), $block = null ) { |
| 16 | if ( isset( $props['name'] ) ) { |
| 17 | $this->name = $props['name']; |
| 18 | unset( $props['name'] ); |
| 19 | } |
| 20 | parent::__construct( $type, $props, $children, $block ); |
| 21 | } |
| 22 | |
| 23 | function __toString() { |
| 24 | $props = $this->getFinalProps(); |
| 25 | if ( ! $this->shouldRender ) { |
| 26 | return ''; |
| 27 | } |
| 28 | |
| 29 | return Registry::getInstance()->createElement( $this->tagName( $props ), $props, $this->children ) . ''; |
| 30 | } |
| 31 | |
| 32 | function getFinalProps() { |
| 33 | return $this->mergeProps( |
| 34 | parent::getProps(), |
| 35 | array( |
| 36 | 'className' => $this->getClassName(), |
| 37 | 'innerHTML' => $this->innerHTML, |
| 38 | ), |
| 39 | $this->mappedProps() |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | function getClassName() { |
| 44 | $classes = parent::getClassName(); |
| 45 | return $this->getClasses( $classes ); |
| 46 | } |
| 47 | |
| 48 | function getClasses( $extraClasses = array() ) { |
| 49 | |
| 50 | $classes = LodashBasic::concat( |
| 51 | array( |
| 52 | 'position-relative', |
| 53 | $this->getBemClass( $this->block->name() ), |
| 54 | ), |
| 55 | $extraClasses |
| 56 | ); |
| 57 | $blockElement = $this->block->elements[0]; |
| 58 | $disableStyleClasses = property_exists( $blockElement, 'disableStyleClasses' ) && $blockElement->disableStyleClasses; |
| 59 | |
| 60 | if ( ! $disableStyleClasses ) { |
| 61 | $classes = LodashBasic::concat( |
| 62 | $classes, |
| 63 | array( |
| 64 | $this->getSharedClass( $this->block->getMainAttributeProp( 'styleRef' ) ), |
| 65 | $this->getLocalClasses( $this->block->localId() ), |
| 66 | ) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | return $classes; |
| 71 | } |
| 72 | |
| 73 | function getBemClass( $blockName ) { |
| 74 | return 'wp-block-' . str_replace( '/', '-', $blockName ) . '__' . $this->name; |
| 75 | } |
| 76 | |
| 77 | function getSharedClass( $styleRef ) { |
| 78 | $style_prefix = apply_filters( 'kubio/element-style-class-prefix', 'style-' ); |
| 79 | |
| 80 | return $style_prefix . $styleRef . '-' . $this->name; |
| 81 | } |
| 82 | |
| 83 | function getLocalClasses( $localId = null ) { |
| 84 | $style_prefix = apply_filters( 'kubio/element-style-class-prefix', 'style-' ); |
| 85 | $style_prefix = "{$style_prefix}local-"; |
| 86 | return $localId ? $style_prefix . $localId . '-' . $this->name : false; |
| 87 | } |
| 88 | |
| 89 | function mappedProps() { |
| 90 | $mappedProps = array(); |
| 91 | if ( $this->block ) { |
| 92 | $props = $this->getConfig( 'props', array() ); |
| 93 | $mappedProps = array( 'className' => $this->getConfig( 'classes', array() ) ); |
| 94 | $mapped = LodashBasic::get( |
| 95 | apply_filters( 'kubio/blocks/element_props_map', $this->block->mapPropsToElementsWithDefaults(), $this->block ), |
| 96 | $this->name |
| 97 | ); |
| 98 | $mappedProps = $this->mergeProps( $props, $mappedProps, $mapped ); |
| 99 | } |
| 100 | |
| 101 | return $mappedProps; |
| 102 | } |
| 103 | |
| 104 | function getConfig( $path, $defaultValue = null ) { |
| 105 | return $this->block->getStyledElementConfig( $this->name, $path, $defaultValue ); |
| 106 | } |
| 107 | |
| 108 | function tagName( $props = null ) { |
| 109 | if ( $props && isset( $props['tag'] ) ) { |
| 110 | return $props['tag']; |
| 111 | } |
| 112 | |
| 113 | return $this->getConfig( 'tag', $this->type ); |
| 114 | } |
| 115 | } |
| 116 |