| @@ -1,0 +1,314 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\Countdown; | |
| 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\Border; | |
| 14 | +use ABlocks\Controls\BoxShadow; | |
| 15 | +use ABlocks\Controls\Range; | |
| 16 | +use ABlocks\Controls\Color; | |
| 17 | +use ABlocks\Controls\Dimensions; | |
| 18 | + | |
| 19 | +class Block extends BlockBaseAbstract { | |
| 20 | + protected $block_name = 'countdown'; | |
| 21 | + | |
| 22 | + public function build_css_v1( $attributes ) { | |
| 23 | + $css_generator = new CssGenerator( $attributes ); | |
| 24 | + | |
| 25 | + $css_generator->add_class_styles( | |
| 26 | + '{{WRAPPER}}', | |
| 27 | + $this->get_wrapper_css( $attributes ), | |
| 28 | + $this->get_wrapper_css( $attributes, 'Tablet' ), | |
| 29 | + $this->get_wrapper_css( $attributes, 'Mobile' ), | |
| 30 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_wrapper_css( $attributes, $device ); } ) | |
| 31 | + ); | |
| 32 | + | |
| 33 | + $css_generator->add_class_styles( | |
| 34 | + '{{WRAPPER}} .ablocks-block-container', | |
| 35 | + $this->get_countdown_items_css( $attributes ), | |
| 36 | + $this->get_countdown_items_css( $attributes, 'Tablet' ), | |
| 37 | + $this->get_countdown_items_css( $attributes, 'Mobile' ), | |
| 38 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_countdown_items_css( $attributes, $device ); } ) | |
| 39 | + ); | |
| 40 | + | |
| 41 | + $css_generator->add_class_styles( | |
| 42 | + '{{WRAPPER}} .ablocks-countdown__item', | |
| 43 | + $this->get_countdown_item_css( $attributes ), | |
| 44 | + $this->get_countdown_item_css( $attributes, 'Tablet' ), | |
| 45 | + $this->get_countdown_item_css( $attributes, 'Mobile' ), | |
| 46 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_countdown_item_css( $attributes, $device ); } ) | |
| 47 | + ); | |
| 48 | + $css_generator->add_class_styles( | |
| 49 | + '{{WRAPPER}} .ablocks-countdown__item:hover', | |
| 50 | + $this->get_countdown_item_hover_css( $attributes ), | |
| 51 | + $this->get_countdown_item_hover_css( $attributes, 'Tablet' ), | |
| 52 | + $this->get_countdown_item_hover_css( $attributes, 'Mobile' ), | |
| 53 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_countdown_item_hover_css( $attributes, $device ); } ) | |
| 54 | + ); | |
| 55 | + | |
| 56 | + $css_generator->add_class_styles( | |
| 57 | + '{{WRAPPER}} .ablocks-countdown__item .ablocks-countdown-label', | |
| 58 | + $this->get_label_css( $attributes ), | |
| 59 | + $this->get_label_css( $attributes, 'Tablet' ), | |
| 60 | + $this->get_label_css( $attributes, 'Mobile' ), | |
| 61 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_label_css( $attributes, $device ); } ) | |
| 62 | + ); | |
| 63 | + | |
| 64 | + $css_generator->add_class_styles( | |
| 65 | + '{{WRAPPER}} .ablocks-countdown__item .ablocks-countdown-value', | |
| 66 | + $this->get_number_css( $attributes ), | |
| 67 | + $this->get_number_css( $attributes, 'Tablet' ), | |
| 68 | + $this->get_number_css( $attributes, 'Mobile' ), | |
| 69 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_number_css( $attributes, $device ); } ) | |
| 70 | + ); | |
| 71 | + | |
| 72 | + $css_generator->add_class_styles( | |
| 73 | + '{{WRAPPER}} .ablocks-countdown__separator', | |
| 74 | + $this->get_separator_css( $attributes ), | |
| 75 | + $this->get_separator_css( $attributes, 'Tablet' ), | |
| 76 | + $this->get_separator_css( $attributes, 'Mobile' ), | |
| 77 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_separator_css( $attributes, $device ); } ) | |
| 78 | + ); | |
| 79 | + | |
| 80 | + return $css_generator->generate_css(); | |
| 81 | + } | |
| 82 | + public function build_css_v2( $attributes ) { | |
| 83 | + $css_generator = new CssGeneratorV2( $attributes, $this->block_name ); | |
| 84 | + | |
| 85 | + $css_generator->add_class_styles( | |
| 86 | + '{{WRAPPER}}', | |
| 87 | + $this->get_wrapper_css( $attributes ), | |
| 88 | + $this->get_wrapper_css( $attributes, 'Tablet' ), | |
| 89 | + $this->get_wrapper_css( $attributes, 'Mobile' ), | |
| 90 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_wrapper_css( $attributes, $device ); } ) | |
| 91 | + ); | |
| 92 | + $css_generator->add_class_styles( | |
| 93 | + '{{WRAPPER}}.ablocks-block--countdown:not(.ablocks-has-container), | |
| 94 | + {{WRAPPER}}.ablocks-block--countdown .ablocks-block-container', | |
| 95 | + $this->get_countdown_items_css( $attributes, '' ), | |
| 96 | + $this->get_countdown_items_css( $attributes, 'Tablet' ), | |
| 97 | + $this->get_countdown_items_css( $attributes, 'Mobile' ), | |
| 98 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_countdown_items_css( $attributes, $device ); } ) | |
| 99 | + ); | |
| 100 | + | |
| 101 | + $css_generator->add_class_styles( | |
| 102 | + '{{WRAPPER}} .ablocks-countdown__item', | |
| 103 | + $this->get_countdown_item_css( $attributes ), | |
| 104 | + $this->get_countdown_item_css( $attributes, 'Tablet' ), | |
| 105 | + $this->get_countdown_item_css( $attributes, 'Mobile' ), | |
| 106 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_countdown_item_css( $attributes, $device ); } ) | |
| 107 | + ); | |
| 108 | + $css_generator->add_class_styles( | |
| 109 | + '{{WRAPPER}} .ablocks-countdown__item:hover', | |
| 110 | + $this->get_countdown_item_hover_css( $attributes ), | |
| 111 | + $this->get_countdown_item_hover_css( $attributes, 'Tablet' ), | |
| 112 | + $this->get_countdown_item_hover_css( $attributes, 'Mobile' ), | |
| 113 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_countdown_item_hover_css( $attributes, $device ); } ) | |
| 114 | + ); | |
| 115 | + | |
| 116 | + $css_generator->add_class_styles( | |
| 117 | + '{{WRAPPER}} .ablocks-countdown__item .ablocks-countdown-label', | |
| 118 | + $this->get_label_css( $attributes ), | |
| 119 | + $this->get_label_css( $attributes, 'Tablet' ), | |
| 120 | + $this->get_label_css( $attributes, 'Mobile' ), | |
| 121 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_label_css( $attributes, $device ); } ) | |
| 122 | + ); | |
| 123 | + | |
| 124 | + $css_generator->add_class_styles( | |
| 125 | + '{{WRAPPER}} .ablocks-countdown__item .ablocks-countdown-value', | |
| 126 | + $this->get_number_css( $attributes ), | |
| 127 | + $this->get_number_css( $attributes, 'Tablet' ), | |
| 128 | + $this->get_number_css( $attributes, 'Mobile' ), | |
| 129 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_number_css( $attributes, $device ); } ) | |
| 130 | + ); | |
| 131 | + | |
| 132 | + $css_generator->add_class_styles( | |
| 133 | + '{{WRAPPER}} .ablocks-countdown__separator', | |
| 134 | + $this->get_separator_css( $attributes ), | |
| 135 | + $this->get_separator_css( $attributes, 'Tablet' ), | |
| 136 | + $this->get_separator_css( $attributes, 'Mobile' ), | |
| 137 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_separator_css( $attributes, $device ); } ) | |
| 138 | + ); | |
| 139 | + | |
| 140 | + return $css_generator->generate_css(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + public function build_css( $attributes ) { | |
| 144 | + if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { | |
| 145 | + return $this->build_css_v2( $attributes ); | |
| 146 | + } | |
| 147 | + return $this->build_css_v1( $attributes ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + public function get_wrapper_css( $attributes, $device = '' ) { | |
| 151 | + $alignment = ! empty( $attributes['alignment'] ) ? $attributes['alignment'] : ''; | |
| 152 | + return Alignment::get_css( $alignment, 'text-align', $device ); | |
| 153 | + } | |
| 154 | + | |
| 155 | + public function get_countdown_items_css( $attributes, $device = '' ) { | |
| 156 | + $css = []; | |
| 157 | + | |
| 158 | + if ( ! empty( $attributes['orient'][ 'value' . $device ] ) ) { | |
| 159 | + $css['flex-direction'] = $attributes['orient'][ 'value' . $device ]; | |
| 160 | + } | |
| 161 | + if ( ! empty( $attributes['justificationAlign'][ 'value' . $device ] ) ) { | |
| 162 | + $css['justify-content'] = $attributes['justificationAlign'][ 'value' . $device ]; | |
| 163 | + } | |
| 164 | + if ( ! empty( $attributes['alignment'][ 'value' . $device ] ) ) { | |
| 165 | + $css['align-items'] = $attributes['alignment'][ 'value' . $device ]; | |
| 166 | + } | |
| 167 | + if ( ! empty( $attributes['wrapping'][ 'value' . $device ] ) ) { | |
| 168 | + $css['flex-wrap'] = $attributes['wrapping'][ 'value' . $device ]; | |
| 169 | + } | |
| 170 | + | |
| 171 | + return array_merge( | |
| 172 | + Range::get_css([ | |
| 173 | + 'attributeValue' => $attributes['boxSize'], | |
| 174 | + 'attribute_object_key' => 'value', | |
| 175 | + 'isResponsive' => true, | |
| 176 | + 'defaultValue' => 130, | |
| 177 | + 'hasUnit' => true, | |
| 178 | + 'unitDefaultValue' => 'px', | |
| 179 | + 'property' => 'min-height', | |
| 180 | + 'device' => $device, | |
| 181 | + ]), | |
| 182 | + Range::get_css([ | |
| 183 | + 'attributeValue' => $attributes['boxRowGap'], | |
| 184 | + 'attribute_object_key' => 'value', | |
| 185 | + 'isResponsive' => true, | |
| 186 | + 'defaultValue' => 0, | |
| 187 | + 'hasUnit' => true, | |
| 188 | + 'unitDefaultValue' => 'px', | |
| 189 | + 'property' => 'row-gap', | |
| 190 | + 'device' => $device, | |
| 191 | + ]), | |
| 192 | + Range::get_css([ | |
| 193 | + 'attributeValue' => $attributes['boxColumnGap'], | |
| 194 | + 'attribute_object_key' => 'value', | |
| 195 | + 'isResponsive' => true, | |
| 196 | + 'defaultValue' => 0, | |
| 197 | + 'hasUnit' => true, | |
| 198 | + 'unitDefaultValue' => 'px', | |
| 199 | + 'property' => 'column-gap', | |
| 200 | + 'device' => $device, | |
| 201 | + ]), | |
| 202 | + $css, | |
| 203 | + ); | |
| 204 | + } | |
| 205 | + | |
| 206 | + public function get_countdown_item_css( $attributes, $device = '' ) { | |
| 207 | + $css = []; | |
| 208 | + $width_css = []; | |
| 209 | + // $css['padding'] = '0'; | |
| 210 | + $label_position = ! empty( $attributes['labelPosition'] ) ? $attributes['labelPosition'] : ''; | |
| 211 | + $orient = ! empty( $attributes['orient'] ) ? $attributes['orient'] : []; | |
| 212 | + | |
| 213 | + $device_key = $device ? 'value' . ucfirst( $device ) : 'value'; | |
| 214 | + $direction = isset( $orient[ $device_key ] ) ? $orient[ $device_key ] : ( $orient['value'] ?? '' ); | |
| 215 | + | |
| 216 | + if ( in_array( $direction, [ 'column', 'column-reverse' ], true ) ) { | |
| 217 | + $width_css = Range::get_css( [ | |
| 218 | + 'attributeValue' => $attributes['boxSize'], | |
| 219 | + 'attribute_object_key' => 'value', | |
| 220 | + 'isResponsive' => true, | |
| 221 | + 'defaultValue' => 130, | |
| 222 | + 'hasUnit' => true, | |
| 223 | + 'unitDefaultValue' => 'px', | |
| 224 | + 'property' => 'width', | |
| 225 | + 'device' => $device, | |
| 226 | + ] ); | |
| 227 | + } else { | |
| 228 | + $width_css = [ 'width' => '130px' ]; | |
| 229 | + } | |
| 230 | + | |
| 231 | + if ( $label_position ) { | |
| 232 | + $css['flex-direction'] = $label_position; | |
| 233 | + } | |
| 234 | + | |
| 235 | + $border_css = []; | |
| 236 | + if ( isset( $attributes['boxBorder'] ) ) { | |
| 237 | + $border_css = Border::get_css( $attributes['boxBorder'], '', $device ); | |
| 238 | + } | |
| 239 | + | |
| 240 | + return array_merge( | |
| 241 | + [ 'background' => Color::get_css( isset( $attributes['boxBackgroundColor'] ) ? $attributes['boxBackgroundColor'] : '' ) ], | |
| 242 | + $width_css, | |
| 243 | + Range::get_css( [ | |
| 244 | + 'attributeValue' => $attributes['numberAndLabelGap'], | |
| 245 | + 'attribute_object_key' => 'value', | |
| 246 | + 'isResponsive' => true, | |
| 247 | + 'defaultValue' => 5, | |
| 248 | + 'hasUnit' => true, | |
| 249 | + 'unitDefaultValue' => 'px', | |
| 250 | + 'property' => 'gap', | |
| 251 | + 'device' => $device, | |
| 252 | + ] ), | |
| 253 | + $border_css, | |
| 254 | + $css, | |
| 255 | + BoxShadow::get_css( ! empty( $attributes['boxShadow'] ) ? $attributes['boxShadow'] : '', $device ), | |
| 256 | + Dimensions::get_css( $attributes['padding'], 'padding', $device ), | |
| 257 | + ); | |
| 258 | + } | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + public function get_countdown_item_hover_css( $attributes, $device = '' ) { | |
| 264 | + return array_merge( | |
| 265 | + Border::get_hover_css( ! empty( $attributes['boxBorder'] ) ? $attributes['boxBorder'] : [], '', $device ), | |
| 266 | + BoxShadow::get_hover_css( $attributes['boxShadow'], '', $device ) | |
| 267 | + ); | |
| 268 | + } | |
| 269 | + | |
| 270 | + public function get_label_css( $attributes, $device = '' ) { | |
| 271 | + $css = []; | |
| 272 | + if ( $attributes['labelPosition'] === 'column' || $attributes['labelPosition'] === 'column-reverse' ) { | |
| 273 | + $css['width'] = '100%'; | |
| 274 | + $css['flex'] = '1 1 30%'; | |
| 275 | + $css['display'] = 'flex'; | |
| 276 | + $css['justify-content'] = 'center'; | |
| 277 | + $css['align-items'] = 'center'; | |
| 278 | + } | |
| 279 | + $typographyGlobal = ( isset( $attributes['labelTypographyGlobal'] ) ? $attributes['labelTypographyGlobal'] : '' ); | |
| 280 | + return array_merge( | |
| 281 | + [ 'color' => Color::get_css( isset( $attributes['labelColor'] ) ? $attributes['labelColor'] : '' ) ], | |
| 282 | + [ 'background' => Color::get_css( isset( $attributes['labelBgColor'] ) ? $attributes['labelBgColor'] : '' ) ], | |
| 283 | + ! empty( $attributes['labelTypography'] ) ? Typography::get_css( $attributes['labelTypography'], '', $device, $typographyGlobal ) : array(), | |
| 284 | + $css | |
| 285 | + ); | |
| 286 | + } | |
| 287 | + | |
| 288 | + public function get_number_css( $attributes, $device = '' ) { | |
| 289 | + $css = []; | |
| 290 | + if ( $attributes['labelPosition'] === 'column' || $attributes['labelPosition'] === 'column-reverse' ) { | |
| 291 | + $css['width'] = '100%'; | |
| 292 | + $css['flex'] = '1 1 70%'; | |
| 293 | + $css['display'] = 'flex'; | |
| 294 | + $css['justify-content'] = 'center'; | |
| 295 | + $css['align-items'] = 'center'; | |
| 296 | + } | |
| 297 | + $typographyGlobal = ( isset( $attributes['numberTypographyGlobal'] ) ? $attributes['numberTypographyGlobal'] : '' ); | |
| 298 | + return array_merge( | |
| 299 | + [ 'color' => Color::get_css( isset( $attributes['numberColor'] ) ? $attributes['numberColor'] : '' ) ], | |
| 300 | + [ 'background' => Color::get_css( isset( $attributes['numberBgColor'] ) ? $attributes['numberBgColor'] : '' ) ], | |
| 301 | + ! empty( $attributes['numberTypography'] ) ? Typography::get_css( $attributes['numberTypography'], '', $device, $typographyGlobal ) : array(), | |
| 302 | + $css | |
| 303 | + ); | |
| 304 | + } | |
| 305 | + | |
| 306 | + public function get_separator_css( $attributes, $device = '' ) { | |
| 307 | + $typographyGlobal = ( isset( $attributes['separatorTypographyGlobal'] ) ? $attributes['separatorTypographyGlobal'] : '' ); | |
| 308 | + return array_merge( | |
| 309 | + [ 'color' => Color::get_css( isset( $attributes['separatorColor'] ) ? $attributes['separatorColor'] : '' ) ], | |
| 310 | + ! empty( $attributes['separatorTypography'] ) ? Typography::get_css( $attributes['separatorTypography'], '', $device, $typographyGlobal ) : array(), | |
| 311 | + ); | |
| 312 | + } | |
| 313 | +} | |
| 314 | + | |