| @@ -1,0 +1,205 @@ | ||
| 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\Classes\CssGeneratorV2; | |
| 11 | +use ABlocks\Controls\Alignment; | |
| 12 | +use ABlocks\Controls\Typography; | |
| 13 | +use ABlocks\Controls\Range; | |
| 14 | +use ABlocks\Controls\Dimensions; | |
| 15 | +use ABlocks\Controls\Border; | |
| 16 | +use ABlocks\Controls\Color; | |
| 17 | + | |
| 18 | + | |
| 19 | +class Block extends BlockBaseAbstract { | |
| 20 | + protected $block_name = 'toggle'; | |
| 21 | + | |
| 22 | + public function build_css_v1( $attributes ) { | |
| 23 | + $css_generator = new CssGenerator( $attributes ); | |
| 24 | + | |
| 25 | + $css_generator->add_class_styles( | |
| 26 | + '{{WRAPPER}} .ablocks-toggle__topbar', | |
| 27 | + $this->get_toggle_bar_css( $attributes ), | |
| 28 | + $this->get_toggle_bar_css( $attributes, 'Tablet' ), | |
| 29 | + $this->get_toggle_bar_css( $attributes, 'Mobile' ) | |
| 30 | + ); | |
| 31 | + $css_generator->add_class_styles( | |
| 32 | + '{{WRAPPER}} .ablocks-toggle__topbar:hover', | |
| 33 | + $this->get_toggle_bar_hover_css( $attributes ), | |
| 34 | + $this->get_toggle_bar_hover_css( $attributes, 'Tablet' ), | |
| 35 | + $this->get_toggle_bar_hover_css( $attributes, 'Mobile' ) | |
| 36 | + ); | |
| 37 | + $css_generator->add_class_styles( | |
| 38 | + '{{WRAPPER}} .ablocks-toggle__topbar-wrapper', | |
| 39 | + $this->get_toggle_bar_wrapper_css( $attributes ), | |
| 40 | + $this->get_toggle_bar_wrapper_css( $attributes, 'Tablet' ), | |
| 41 | + $this->get_toggle_bar_wrapper_css( $attributes, 'Mobile' ) | |
| 42 | + ); | |
| 43 | + $css_generator->add_class_styles( | |
| 44 | + '{{WRAPPER}} .ablocks-toggle__label', | |
| 45 | + $this->get_toggle_label_css( $attributes ), | |
| 46 | + $this->get_toggle_label_css( $attributes, 'Tablet' ), | |
| 47 | + $this->get_toggle_label_css( $attributes, 'Mobile' ) | |
| 48 | + ); | |
| 49 | + $css_generator->add_class_styles( | |
| 50 | + '{{WRAPPER}} .ablocks-toggle__label--active', | |
| 51 | + $this->get_toggle_label_active_css( $attributes ) | |
| 52 | + ); | |
| 53 | + | |
| 54 | + if ( ! empty( $attributes['toggleNormalBgColor'] ) ) { | |
| 55 | + $css_generator->add_class_styles( | |
| 56 | + '{{WRAPPER}} .ablocks-toggle__slider', | |
| 57 | + [ 'background-color' => $attributes['toggleNormalBgColor'] ] | |
| 58 | + ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + if ( ! empty( $attributes['toggleNormalColor'] ) ) { | |
| 62 | + $css_generator->add_class_styles( | |
| 63 | + '{{WRAPPER}} .ablocks-toggle__slider:before', | |
| 64 | + [ 'background-color' => $attributes['toggleNormalColor'] ] | |
| 65 | + ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + if ( ! empty( $attributes['toggleActiveBgColor'] ) ) { | |
| 69 | + $css_generator->add_class_styles( | |
| 70 | + '{{WRAPPER}} input.ablocks-toggle__checkbox:checked + .ablocks-toggle__slider', | |
| 71 | + [ 'background-color' => $attributes['toggleActiveBgColor'] ] | |
| 72 | + ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + if ( ! empty( $attributes['toggleActiveColor'] ) ) { | |
| 76 | + $css_generator->add_class_styles( | |
| 77 | + '{{WRAPPER}} input.ablocks-toggle__checkbox:checked + .ablocks-toggle__slider:before', | |
| 78 | + [ 'background-color' => $attributes['toggleActiveColor'] ] | |
| 79 | + ); | |
| 80 | + } | |
| 81 | + | |
| 82 | + return $css_generator->generate_css(); | |
| 83 | + } | |
| 84 | + public function build_css_v2( $attributes ) { | |
| 85 | + $css_generator = new CssGeneratorV2( $attributes ); | |
| 86 | + | |
| 87 | + $css_generator->add_class_styles( | |
| 88 | + '{{WRAPPER}} .ablocks-toggle__topbar', | |
| 89 | + $this->get_toggle_bar_css( $attributes ), | |
| 90 | + $this->get_toggle_bar_css( $attributes, 'Tablet' ), | |
| 91 | + $this->get_toggle_bar_css( $attributes, 'Mobile' ) | |
| 92 | + ); | |
| 93 | + $css_generator->add_class_styles( | |
| 94 | + '{{WRAPPER}} .ablocks-toggle__topbar:hover', | |
| 95 | + $this->get_toggle_bar_hover_css( $attributes ), | |
| 96 | + $this->get_toggle_bar_hover_css( $attributes, 'Tablet' ), | |
| 97 | + $this->get_toggle_bar_hover_css( $attributes, 'Mobile' ) | |
| 98 | + ); | |
| 99 | + $css_generator->add_class_styles( | |
| 100 | + '{{WRAPPER}} .ablocks-toggle__topbar-wrapper', | |
| 101 | + $this->get_toggle_bar_wrapper_css( $attributes ), | |
| 102 | + $this->get_toggle_bar_wrapper_css( $attributes, 'Tablet' ), | |
| 103 | + $this->get_toggle_bar_wrapper_css( $attributes, 'Mobile' ) | |
| 104 | + ); | |
| 105 | + $css_generator->add_class_styles( | |
| 106 | + '{{WRAPPER}} .ablocks-toggle__label', | |
| 107 | + $this->get_toggle_label_css( $attributes ), | |
| 108 | + $this->get_toggle_label_css( $attributes, 'Tablet' ), | |
| 109 | + $this->get_toggle_label_css( $attributes, 'Mobile' ) | |
| 110 | + ); | |
| 111 | + $css_generator->add_class_styles( | |
| 112 | + '{{WRAPPER}} .ablocks-toggle__label--active', | |
| 113 | + $this->get_toggle_label_active_css( $attributes ) | |
| 114 | + ); | |
| 115 | + | |
| 116 | + if ( ! empty( $attributes['toggleNormalBgColor'] ) ) { | |
| 117 | + $css_generator->add_class_styles( | |
| 118 | + '{{WRAPPER}} .ablocks-toggle__slider', | |
| 119 | + [ 'background-color' => Color::get_css( isset( $attributes['toggleNormalBgColor'] ) ? $attributes['toggleNormalBgColor'] : '' ) ] | |
| 120 | + ); | |
| 121 | + } | |
| 122 | + | |
| 123 | + if ( ! empty( $attributes['toggleNormalColor'] ) ) { | |
| 124 | + $css_generator->add_class_styles( | |
| 125 | + '{{WRAPPER}} .ablocks-toggle__slider:before', | |
| 126 | + [ 'background-color' => Color::get_css( isset( $attributes['toggleNormalColor'] ) ? $attributes['toggleNormalColor'] : '' ) ] | |
| 127 | + ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + if ( ! empty( $attributes['toggleActiveBgColor'] ) ) { | |
| 131 | + $css_generator->add_class_styles( | |
| 132 | + '{{WRAPPER}} input.ablocks-toggle__checkbox:checked + .ablocks-toggle__slider', | |
| 133 | + [ 'background-color' => Color::get_css( isset( $attributes['toggleActiveBgColor'] ) ? $attributes['toggleActiveBgColor'] : '' ) ] | |
| 134 | + ); | |
| 135 | + } | |
| 136 | + | |
| 137 | + if ( ! empty( $attributes['toggleActiveColor'] ) ) { | |
| 138 | + $css_generator->add_class_styles( | |
| 139 | + '{{WRAPPER}} input.ablocks-toggle__checkbox:checked + .ablocks-toggle__slider:before', | |
| 140 | + [ 'background-color' => Color::get_css( isset( $attributes['toggleActiveColor'] ) ? $attributes['toggleActiveColor'] : '' ) ] | |
| 141 | + ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + return $css_generator->generate_css(); | |
| 145 | + } | |
| 146 | + public function build_css( $attributes ) { | |
| 147 | + if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { | |
| 148 | + return $this->build_css_v2( $attributes ); | |
| 149 | + } | |
| 150 | + return $this->build_css_v1( $attributes ); | |
| 151 | + } | |
| 152 | + | |
| 153 | + public function get_toggle_bar_css( $attributes, $device = '' ) { | |
| 154 | + | |
| 155 | + return array_merge( | |
| 156 | + [ 'background-color' => Color::get_css( isset( $attributes['toggleBarBgColor'] ) ? $attributes['toggleBarBgColor'] : '' ) ], | |
| 157 | + Range::get_css([ | |
| 158 | + 'attributeValue' => $attributes['space'], | |
| 159 | + 'attribute_object_key' => 'value', | |
| 160 | + 'isResponsive' => true, | |
| 161 | + 'defaultValue' => 10, | |
| 162 | + 'unitDefaultValue' => 'px', | |
| 163 | + 'property' => 'margin-bottom', | |
| 164 | + 'device' => $device, | |
| 165 | + ]), | |
| 166 | + isset( $attributes['alignment'] ) ? Alignment::get_css( $attributes['alignment'], 'text-align', $device ) : [], | |
| 167 | + isset( $attributes['toggleBarBorder'] ) ? Border::get_css( $attributes['toggleBarBorder'], '', $device ) : [], | |
| 168 | + isset( $attributes['toggleBarPadding'] ) ? Dimensions::get_css( $attributes['toggleBarPadding'], 'padding', $device ) : [], | |
| 169 | + ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + public function get_toggle_bar_wrapper_css( $attributes, $device = '' ) { | |
| 173 | + $toggleBarWrapperCSS = Range::get_css([ | |
| 174 | + 'attributeValue' => $attributes['gap'], | |
| 175 | + 'attribute_object_key' => 'value', | |
| 176 | + 'isResponsive' => true, | |
| 177 | + 'defaultValue' => 10, | |
| 178 | + 'unitDefaultValue' => 'px', | |
| 179 | + 'property' => 'gap', | |
| 180 | + 'device' => $device, | |
| 181 | + ]); | |
| 182 | + | |
| 183 | + if ( ! empty( $attributes['toggleDirection'] ) ) { | |
| 184 | + $toggleBarWrapperCSS['flex-direction'] = $attributes['toggleDirection']; | |
| 185 | + } | |
| 186 | + return $toggleBarWrapperCSS; | |
| 187 | + } | |
| 188 | + | |
| 189 | + public function get_toggle_bar_hover_css( $attributes, $device = '' ) { | |
| 190 | + return isset( $attributes['toggleBarBorder'] ) ? Border::get_hover_css( $attributes['toggleBarBorder'], $device ) : []; | |
| 191 | + } | |
| 192 | + | |
| 193 | + public function get_toggle_label_css( $attributes, $device = '' ) { | |
| 194 | + $typographyValueGlobal = ! empty( $attributes['labelTypographyGlobal'] ) ? $attributes['labelTypographyGlobal'] : []; | |
| 195 | + $labelCSS = isset( $attributes['labelTypography'] ) ? Typography::get_css( $attributes['labelTypography'], '', $device, $typographyValueGlobal ) : []; | |
| 196 | + return array_merge( | |
| 197 | + [ 'color' => Color::get_css( isset( $attributes['labelNormalColor'] ) ? $attributes['labelNormalColor'] : '' ) ], | |
| 198 | + $labelCSS | |
| 199 | + ); | |
| 200 | + } | |
| 201 | + | |
| 202 | + public function get_toggle_label_active_css( $attributes ) { | |
| 203 | + return [ 'color' => Color::get_css( isset( $attributes['labelActiveColor'] ) ? $attributes['labelActiveColor'] : '' ) ]; | |
| 204 | + } | |
| 205 | +} | |