| 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 |
); |
| 29 |
$css_generator->add_class_styles( |
| 30 |
'{{WRAPPER}} .ablocks-button', |
| 31 |
$this->get_button_css( $attributes ), |
| 32 |
$this->get_button_css( $attributes, 'Tablet' ), |
| 33 |
$this->get_button_css( $attributes, 'Mobile' ) |
| 34 |
); |
| 35 |
|
| 36 |
return $css_generator->generate_css(); |
| 37 |
} |
| 38 |
public function build_css_v2( $attributes ) { |
| 39 |
|
| 40 |
$css_generator = new CssGeneratorV2( $attributes, $this->block_name ); |
| 41 |
|
| 42 |
$css_generator->add_class_styles( |
| 43 |
'{{WRAPPER}}:not(.ablocks-has-block-container), {{WRAPPER}}.ablocks-block--dual-button > .ablocks-block-container', |
| 44 |
$this->getDualButtonCss( $attributes ), |
| 45 |
$this->getDualButtonCss( $attributes, 'Tablet' ), |
| 46 |
$this->getDualButtonCss( $attributes, 'Mobile' ) |
| 47 |
); |
| 48 |
$css_generator->add_class_styles( |
| 49 |
'{{WRAPPER}} .ablocks-button', |
| 50 |
$this->get_button_css( $attributes ), |
| 51 |
$this->get_button_css( $attributes, 'Tablet' ), |
| 52 |
$this->get_button_css( $attributes, 'Mobile' ) |
| 53 |
); |
| 54 |
|
| 55 |
return $css_generator->generate_css(); |
| 56 |
} |
| 57 |
public function build_css( $attributes ) { |
| 58 |
if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { |
| 59 |
return $this->build_css_v2( $attributes ); |
| 60 |
} |
| 61 |
return $this->build_css_v1( $attributes ); |
| 62 |
} |
| 63 |
|
| 64 |
public function getDualButtonCss( $attributes, $device = '' ) { |
| 65 |
$css = []; |
| 66 |
$stack = isset( $attributes['stack'] ) ? $attributes['stack'] : ''; |
| 67 |
|
| 68 |
$css['flex-wrap'] = 'wrap'; |
| 69 |
if ( $stack === 'vertical' ) { |
| 70 |
$css['flex-direction'] = 'column'; |
| 71 |
$css['display'] = 'flex'; |
| 72 |
} |
| 73 |
|
| 74 |
if ( $stack === 'horizontal' ) { |
| 75 |
$css['flex-direction'] = 'row'; |
| 76 |
$css['display'] = 'flex'; |
| 77 |
} |
| 78 |
return array_merge( |
| 79 |
Range::get_css([ |
| 80 |
'attributeValue' => $attributes['gap'], |
| 81 |
'attribute_object_key' => 'value', |
| 82 |
'isResponsive' => true, |
| 83 |
'defaultValue' => 20, |
| 84 |
'hasUnit' => true, |
| 85 |
'unitDefaultValue' => 'px', |
| 86 |
'property' => 'gap', |
| 87 |
'device' => $device, |
| 88 |
]), |
| 89 |
$css, |
| 90 |
isset( $attributes['alignment'] ) ? Alignment::get_css( $attributes['alignment'], $attributes['stack'] === 'horizontal' ? 'justify-content' : 'align-items', $device ) : [], |
| 91 |
); |
| 92 |
} |
| 93 |
public function get_button_css( $attributes, $device = '' ) { |
| 94 |
$css = []; |
| 95 |
$typographyValueGlobal = ! empty( $attributes['typographyGlobal'] ) ? $attributes['typographyGlobal'] : ''; |
| 96 |
return array_merge( |
| 97 |
$css, |
| 98 |
Typography::get_css( $attributes['typography'], '', $device, $typographyValueGlobal ), |
| 99 |
); |
| 100 |
} |
| 101 |
} |
| 102 |
|