| 1 |
<?php |
| 2 |
namespace ABlocks\Blocks\Toggle; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\BlockBaseAbstract; |
| 9 |
use ABlocks\Classes\CssGenerator; |
| 10 |
use ABlocks\Controls\Alignment; |
| 11 |
use ABlocks\Controls\Typography; |
| 12 |
use ABlocks\Controls\Range; |
| 13 |
use ABlocks\Controls\Dimensions; |
| 14 |
use ABlocks\Controls\Border; |
| 15 |
|
| 16 |
|
| 17 |
class Block extends BlockBaseAbstract { |
| 18 |
protected $block_name = 'toggle'; |
| 19 |
|
| 20 |
public function build_css( $attributes ) { |
| 21 |
$css_generator = new CssGenerator( $attributes ); |
| 22 |
|
| 23 |
$css_generator->add_class_styles( |
| 24 |
'{{WRAPPER}} .ablocks-toggle__topbar', |
| 25 |
$this->get_toggle_bar_css( $attributes ), |
| 26 |
$this->get_toggle_bar_css( $attributes, 'Tablet' ), |
| 27 |
$this->get_toggle_bar_css( $attributes, 'Mobile' ) |
| 28 |
); |
| 29 |
$css_generator->add_class_styles( |
| 30 |
'{{WRAPPER}} .ablocks-toggle__topbar:hover', |
| 31 |
$this->get_toggle_bar_hover_css( $attributes ), |
| 32 |
$this->get_toggle_bar_hover_css( $attributes, 'Tablet' ), |
| 33 |
$this->get_toggle_bar_hover_css( $attributes, 'Mobile' ) |
| 34 |
); |
| 35 |
$css_generator->add_class_styles( |
| 36 |
'{{WRAPPER}} .ablocks-toggle__topbar-wrapper', |
| 37 |
$this->get_toggle_bar_wrapper_css( $attributes ), |
| 38 |
$this->get_toggle_bar_wrapper_css( $attributes, 'Tablet' ), |
| 39 |
$this->get_toggle_bar_wrapper_css( $attributes, 'Mobile' ) |
| 40 |
); |
| 41 |
$css_generator->add_class_styles( |
| 42 |
'{{WRAPPER}} .ablocks-toggle__label', |
| 43 |
$this->get_toggle_label_css( $attributes ), |
| 44 |
$this->get_toggle_label_css( $attributes, 'Tablet' ), |
| 45 |
$this->get_toggle_label_css( $attributes, 'Mobile' ) |
| 46 |
); |
| 47 |
$css_generator->add_class_styles( |
| 48 |
'{{WRAPPER}} .ablocks-toggle__label--active', |
| 49 |
$this->get_toggle_label_active_css( $attributes ) |
| 50 |
); |
| 51 |
|
| 52 |
if ( ! empty( $attributes['toggleNormalBgColor'] ) ) { |
| 53 |
$css_generator->add_class_styles( |
| 54 |
'{{WRAPPER}} .ablocks-toggle__slider', |
| 55 |
[ 'background-color' => $attributes['toggleNormalBgColor'] ] |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! empty( $attributes['toggleNormalColor'] ) ) { |
| 60 |
$css_generator->add_class_styles( |
| 61 |
'{{WRAPPER}} .ablocks-toggle__slider:before', |
| 62 |
[ 'background-color' => $attributes['toggleNormalColor'] ] |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! empty( $attributes['toggleActiveBgColor'] ) ) { |
| 67 |
$css_generator->add_class_styles( |
| 68 |
'{{WRAPPER}} input.ablocks-toggle__checkbox:checked + .ablocks-toggle__slider', |
| 69 |
[ 'background-color' => $attributes['toggleActiveBgColor'] ] |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! empty( $attributes['toggleActiveColor'] ) ) { |
| 74 |
$css_generator->add_class_styles( |
| 75 |
'{{WRAPPER}} input.ablocks-toggle__checkbox:checked + .ablocks-toggle__slider:before', |
| 76 |
[ 'background-color' => $attributes['toggleActiveColor'] ] |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
return $css_generator->generate_css(); |
| 81 |
} |
| 82 |
|
| 83 |
public function get_toggle_bar_css( $attributes, $device = '' ) { |
| 84 |
$toggleBarCSS = []; |
| 85 |
if ( ! empty( $attributes['toggleBarBgColor'] ) ) { |
| 86 |
$toggleBarCSS['background-color'] = $attributes['toggleBarBgColor']; |
| 87 |
} |
| 88 |
return array_merge( |
| 89 |
$toggleBarCSS, |
| 90 |
Range::get_css([ |
| 91 |
'attributeValue' => $attributes['space'], |
| 92 |
'attribute_object_key' => 'value', |
| 93 |
'isResponsive' => true, |
| 94 |
'defaultValue' => 0, |
| 95 |
'unitDefaultValue' => 'px', |
| 96 |
'property' => 'margin-bottom', |
| 97 |
'device' => $device, |
| 98 |
]), |
| 99 |
isset( $attributes['alignment'] ) ? Alignment::get_css( $attributes['alignment'], 'text-align', $device ) : [], |
| 100 |
isset( $attributes['toggleBarBorder'] ) ? Border::get_css( $attributes['toggleBarBorder'], '', $device ) : [], |
| 101 |
isset( $attributes['toggleBarPadding'] ) ? Dimensions::get_css( $attributes['toggleBarPadding'], 'padding', $device ) : [], |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
public function get_toggle_bar_wrapper_css( $attributes, $device = '' ) { |
| 106 |
$toggleBarWrapperCSS = Range::get_css([ |
| 107 |
'attributeValue' => $attributes['gap'], |
| 108 |
'attribute_object_key' => 'value', |
| 109 |
'isResponsive' => true, |
| 110 |
'defaultValue' => 0, |
| 111 |
'unitDefaultValue' => 'px', |
| 112 |
'property' => 'gap', |
| 113 |
'device' => $device, |
| 114 |
]); |
| 115 |
|
| 116 |
if ( ! empty( $attributes['toggleDirection'] ) ) { |
| 117 |
$toggleBarWrapperCSS['flex-direction'] = $attributes['toggleDirection']; |
| 118 |
} |
| 119 |
return $toggleBarWrapperCSS; |
| 120 |
} |
| 121 |
|
| 122 |
public function get_toggle_bar_hover_css( $attributes, $device = '' ) { |
| 123 |
return isset( $attributes['toggleBarBorder'] ) ? Border::get_hover_css( $attributes['toggleBarBorder'], $device ) : []; |
| 124 |
} |
| 125 |
|
| 126 |
public function get_toggle_label_css( $attributes, $device = '' ) { |
| 127 |
$labelCSS = isset( $attributes['labelTypography'] ) ? Typography::get_css( $attributes['labelTypography'], '', $device ) : []; |
| 128 |
if ( ! empty( $attributes['labelNormalColor'] ) ) { |
| 129 |
$labelCSS['color'] = $attributes['labelNormalColor']; |
| 130 |
} |
| 131 |
return $labelCSS; |
| 132 |
} |
| 133 |
|
| 134 |
public function get_toggle_label_active_css( $attributes ) { |
| 135 |
$labelActiveCSS = []; |
| 136 |
|
| 137 |
if ( ! empty( $attributes['labelActiveColor'] ) ) { |
| 138 |
$labelActiveCSS['color'] = $attributes['labelActiveColor']; |
| 139 |
} |
| 140 |
|
| 141 |
return $labelActiveCSS; |
| 142 |
} |
| 143 |
} |
| 144 |
|