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