| @@ -1,0 +1,105 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\DualButton; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Classes\BlockBaseAbstract; | |
| 9 | +use ABlocks\Controls\Typography; | |
| 10 | +use ABlocks\Controls\Alignment; | |
| 11 | +use ABlocks\Classes\CssGenerator; | |
| 12 | +use ABlocks\Classes\CssGeneratorV2; | |
| 13 | +use ABlocks\Controls\Range; | |
| 14 | + | |
| 15 | + | |
| 16 | +class Block extends BlockBaseAbstract { | |
| 17 | + protected $block_name = 'dual-button'; | |
| 18 | + | |
| 19 | + public function build_css_v1( $attributes ) { | |
| 20 | + | |
| 21 | + $css_generator = new CssGenerator( $attributes, $this->block_name ); | |
| 22 | + | |
| 23 | + $css_generator->add_class_styles( | |
| 24 | + '{{WRAPPER}}.ablocks-block--dual-button > .ablocks-block-container', | |
| 25 | + $this->getDualButtonCss( $attributes ), | |
| 26 | + $this->getDualButtonCss( $attributes, 'Tablet' ), | |
| 27 | + $this->getDualButtonCss( $attributes, 'Mobile' ), | |
| 28 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->getDualButtonCss( $attributes, $device ); } ) | |
| 29 | + ); | |
| 30 | + $css_generator->add_class_styles( | |
| 31 | + '{{WRAPPER}} .ablocks-button', | |
| 32 | + $this->get_button_css( $attributes ), | |
| 33 | + $this->get_button_css( $attributes, 'Tablet' ), | |
| 34 | + $this->get_button_css( $attributes, 'Mobile' ), | |
| 35 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_button_css( $attributes, $device ); } ) | |
| 36 | + ); | |
| 37 | + | |
| 38 | + return $css_generator->generate_css(); | |
| 39 | + } | |
| 40 | + public function build_css_v2( $attributes ) { | |
| 41 | + | |
| 42 | + $css_generator = new CssGeneratorV2( $attributes, $this->block_name ); | |
| 43 | + | |
| 44 | + $css_generator->add_class_styles( | |
| 45 | + '{{WRAPPER}}:not(.ablocks-has-block-container), {{WRAPPER}}.ablocks-block--dual-button > .ablocks-block-container', | |
| 46 | + $this->getDualButtonCss( $attributes ), | |
| 47 | + $this->getDualButtonCss( $attributes, 'Tablet' ), | |
| 48 | + $this->getDualButtonCss( $attributes, 'Mobile' ), | |
| 49 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->getDualButtonCss( $attributes, $device ); } ) | |
| 50 | + ); | |
| 51 | + $css_generator->add_class_styles( | |
| 52 | + '{{WRAPPER}} .ablocks-button', | |
| 53 | + $this->get_button_css( $attributes ), | |
| 54 | + $this->get_button_css( $attributes, 'Tablet' ), | |
| 55 | + $this->get_button_css( $attributes, 'Mobile' ), | |
| 56 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_button_css( $attributes, $device ); } ) | |
| 57 | + ); | |
| 58 | + | |
| 59 | + return $css_generator->generate_css(); | |
| 60 | + } | |
| 61 | + public function build_css( $attributes ) { | |
| 62 | + if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { | |
| 63 | + return $this->build_css_v2( $attributes ); | |
| 64 | + } | |
| 65 | + return $this->build_css_v1( $attributes ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + public function getDualButtonCss( $attributes, $device = '' ) { | |
| 69 | + $css = []; | |
| 70 | + $stack = isset( $attributes['stack'] ) ? $attributes['stack'] : ''; | |
| 71 | + | |
| 72 | + $css['flex-wrap'] = 'wrap'; | |
| 73 | + if ( $stack === 'vertical' ) { | |
| 74 | + $css['flex-direction'] = 'column'; | |
| 75 | + $css['display'] = 'flex'; | |
| 76 | + } | |
| 77 | + | |
| 78 | + if ( $stack === 'horizontal' ) { | |
| 79 | + $css['flex-direction'] = 'row'; | |
| 80 | + $css['display'] = 'flex'; | |
| 81 | + } | |
| 82 | + return array_merge( | |
| 83 | + Range::get_css([ | |
| 84 | + 'attributeValue' => $attributes['gap'], | |
| 85 | + 'attribute_object_key' => 'value', | |
| 86 | + 'isResponsive' => true, | |
| 87 | + 'defaultValue' => 20, | |
| 88 | + 'hasUnit' => true, | |
| 89 | + 'unitDefaultValue' => 'px', | |
| 90 | + 'property' => 'gap', | |
| 91 | + 'device' => $device, | |
| 92 | + ]), | |
| 93 | + $css, | |
| 94 | + isset( $attributes['alignment'] ) ? Alignment::get_css( $attributes['alignment'], $attributes['stack'] === 'horizontal' ? 'justify-content' : 'align-items', $device ) : [], | |
| 95 | + ); | |
| 96 | + } | |
| 97 | + public function get_button_css( $attributes, $device = '' ) { | |
| 98 | + $css = []; | |
| 99 | + $typographyValueGlobal = ! empty( $attributes['typographyGlobal'] ) ? $attributes['typographyGlobal'] : ''; | |
| 100 | + return array_merge( | |
| 101 | + $css, | |
| 102 | + Typography::get_css( $attributes['typography'], '', $device, $typographyValueGlobal ), | |
| 103 | + ); | |
| 104 | + } | |
| 105 | +} | |