| @@ -1,0 +1,104 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Controls; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit(); | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Classes\ControlBaseAbstract; | |
| 9 | +use ABlocks\Helper; | |
| 10 | + | |
| 11 | +class Width extends ControlBaseAbstract { | |
| 12 | + | |
| 13 | + public static function get_attribute_default_value( $is_responsive = false ) { | |
| 14 | + if ( $is_responsive ) { | |
| 15 | + return [ | |
| 16 | + 'widthType' => 'default', | |
| 17 | + 'widthTypeTablet' => '', | |
| 18 | + 'widthTypeMobile' => '', | |
| 19 | + 'customWidth' => '', | |
| 20 | + 'customWidthTablet' => '', | |
| 21 | + 'customWidthMobile' => '', | |
| 22 | + 'customWidthUnit' => '%', | |
| 23 | + 'customWidthUnitTablet' => '', | |
| 24 | + 'customWidthUnitMobile' => '', | |
| 25 | + ]; | |
| 26 | + } | |
| 27 | + return [ | |
| 28 | + 'widthType' => 'default', | |
| 29 | + 'customWidth' => '', | |
| 30 | + 'customWidthUnit' => '%', | |
| 31 | + ]; | |
| 32 | + } | |
| 33 | + | |
| 34 | + public static function get_attribute( $attributeName, $isResponsive = false ) { | |
| 35 | + if ( $isResponsive ) { | |
| 36 | + return [ | |
| 37 | + $attributeName => [ | |
| 38 | + 'type' => 'object', | |
| 39 | + 'default' => self::get_attribute_default_value( | |
| 40 | + $isResponsive | |
| 41 | + ), | |
| 42 | + ], | |
| 43 | + ]; | |
| 44 | + } | |
| 45 | + return [ | |
| 46 | + $attributeName => [ | |
| 47 | + 'type' => 'object', | |
| 48 | + 'default' => self::get_attribute_default_value( $isResponsive ), | |
| 49 | + ], | |
| 50 | + ]; | |
| 51 | + } | |
| 52 | + public static function get_css( | |
| 53 | + $attribute_value, | |
| 54 | + $property = '', | |
| 55 | + $device = '' | |
| 56 | + ) { | |
| 57 | + $attribute_value = wp_parse_args( | |
| 58 | + $attribute_value, | |
| 59 | + self::get_attribute_default_value( (bool) $device ) | |
| 60 | + ); | |
| 61 | + | |
| 62 | + $css = []; | |
| 63 | + | |
| 64 | + // Width type (uses responsive fallback) | |
| 65 | + $width_type = Helper::get_responsive_value( | |
| 66 | + $attribute_value, | |
| 67 | + 'widthType', | |
| 68 | + $device, | |
| 69 | + self::get_attribute_default_value() | |
| 70 | + ); | |
| 71 | + | |
| 72 | + if ( ! empty( $width_type ) ) { | |
| 73 | + if ( $width_type === 'full' ) { | |
| 74 | + $css[ $property ] = '100%'; | |
| 75 | + } elseif ( $width_type === 'auto' ) { | |
| 76 | + $css[ $property ] = 'auto'; | |
| 77 | + $css['display'] = 'inline-block'; | |
| 78 | + } elseif ( $width_type === 'custom' ) { | |
| 79 | + // Get custom width with responsive fallback | |
| 80 | + $custom_width = Helper::get_responsive_value( | |
| 81 | + $attribute_value, | |
| 82 | + 'customWidth', | |
| 83 | + $device, | |
| 84 | + self::get_attribute_default_value() | |
| 85 | + ); | |
| 86 | + | |
| 87 | + // Get responsive unit (delegates to your get_unit function) | |
| 88 | + $unit = self::get_unit( | |
| 89 | + [ | |
| 90 | + 'unit' => Helper::get_responsive_value( $attribute_value, 'customWidthUnit', 'Desktop' ), | |
| 91 | + 'unitTablet' => Helper::get_responsive_value( $attribute_value, 'customWidthUnit', 'Tablet' ), | |
| 92 | + 'unitMobile' => Helper::get_responsive_value( $attribute_value, 'customWidthUnit', 'Mobile' ), | |
| 93 | + ], | |
| 94 | + $device | |
| 95 | + ); | |
| 96 | + | |
| 97 | + $css[ $property ] = $custom_width . $unit; | |
| 98 | + }//end if | |
| 99 | + }//end if | |
| 100 | + | |
| 101 | + return $css; | |
| 102 | + } | |
| 103 | + | |
| 104 | +} | |