| @@ -4,11 +4,11 @@ | ||
| 4 | 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | 5 | exit; |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | -use ABlocks\Classes\ControlBaseAbstractTwo; | |
| 8 | +use ABlocks\Helper; | |
| 9 | 9 | |
| 10 | -class Range extends ControlBaseAbstractTwo { | |
| 10 | +class Range { | |
| 11 | 11 | |
| 12 | 12 | public static function get_attribute_default_value( $args ) { |
| 13 | 13 | |
| 14 | 14 | $unitObject = $args['hasUnit'] ? [ |
| @@ -71,11 +71,9 @@ | ||
| 71 | 71 | ]; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | public static function get_css( $args ) { |
| 75 | - if ( ! isset( $args['property'] ) || | |
| 76 | - empty( $args['property'] ) | |
| 77 | - ) { | |
| 75 | + if ( ! isset( $args['property'] ) || empty( $args['property'] ) ) { | |
| 78 | 76 | return []; |
| 79 | 77 | } |
| 80 | 78 | |
| 81 | 79 | // Set default values for missing arguments |
| @@ -91,23 +89,88 @@ | ||
| 91 | 89 | 'device' => '', |
| 92 | 90 | ]); |
| 93 | 91 | |
| 94 | 92 | $value = $args['attributeValue']; |
| 95 | - $css = []; | |
| 93 | + $css = []; | |
| 96 | 94 | |
| 95 | + // ✅ Basic non-responsive + non-unit handling | |
| 97 | 96 | if ( ! $args['isResponsive'] && ! $args['hasUnit'] ) { |
| 98 | 97 | if ( $args['property'] === 'value' ) { |
| 99 | - $css['value'] = $value; | |
| 98 | + $css['value'] = $value; | |
| 100 | 99 | $css['valueUnit'] = $args['unitDefaultValue']; |
| 101 | - } else { | |
| 100 | + } elseif ( '' !== (string) $value ) { | |
| 101 | + // Skip when there's no value — otherwise we emit the unit alone | |
| 102 | + // (e.g. "transition-duration:s"), invalid CSS that bloats every | |
| 103 | + // block's generated stylesheet. | |
| 102 | 104 | $css[ $args['property'] ] = $value . $args['unitDefaultValue']; |
| 103 | 105 | } |
| 104 | 106 | return $css; |
| 105 | 107 | } |
| 106 | 108 | |
| 107 | - // Check and generate CSS for the specified device | |
| 108 | - $deviceValueKey = $args['attributeObjectKey'] . $args['device']; | |
| 109 | - if ( ! empty( $value[ $deviceValueKey ] ) ) { | |
| 109 | + // ✅ Get values per device | |
| 110 | + $desktop_key = $args['attributeObjectKey']; | |
| 111 | + $tablet_key = $args['attributeObjectKey'] . 'Tablet'; | |
| 112 | + $mobile_key = $args['attributeObjectKey'] . 'Mobile'; | |
| 113 | + | |
| 114 | + // A stored 0 is a real value (e.g. "top: 0" at Tablet), so only a missing | |
| 115 | + // or empty-string key counts as unset — matching the editor helper. | |
| 116 | + $desktop_val = self::get_filled_value( $value, $desktop_key ); | |
| 117 | + $tablet_val = self::get_filled_value( $value, $tablet_key ); | |
| 118 | + $mobile_val = self::get_filled_value( $value, $mobile_key ); | |
| 119 | + | |
| 120 | + $device_value = ''; | |
| 121 | + | |
| 122 | + switch ( $args['device'] ) { | |
| 123 | + case '': | |
| 124 | + if ( $desktop_val !== '' ) { | |
| 125 | + $device_value = $desktop_val; | |
| 126 | + } else { | |
| 127 | + $device_value = $args['defaultValue']; | |
| 128 | + } | |
| 129 | + break; | |
| 130 | + | |
| 131 | + case 'Mobile': | |
| 132 | + if ( $mobile_val !== '' ) { | |
| 133 | + $device_value = $mobile_val; | |
| 134 | + } elseif ( $args['defaultValueMobile'] !== '' ) { | |
| 135 | + $device_value = $args['defaultValueMobile']; | |
| 136 | + } elseif ( $tablet_val !== '' ) { | |
| 137 | + $device_value = $tablet_val; | |
| 138 | + } elseif ( $args['defaultValueTablet'] !== '' ) { | |
| 139 | + $device_value = $args['defaultValueTablet']; | |
| 140 | + } elseif ( $desktop_val !== '' ) { | |
| 141 | + $device_value = $desktop_val; | |
| 142 | + } else { | |
| 143 | + $device_value = $args['defaultValue']; | |
| 144 | + } | |
| 145 | + break; | |
| 146 | + | |
| 147 | + case 'Tablet': | |
| 148 | + if ( $tablet_val !== '' ) { | |
| 149 | + $device_value = $tablet_val; | |
| 150 | + } elseif ( $args['defaultValueTablet'] !== '' ) { | |
| 151 | + $device_value = $args['defaultValueTablet']; | |
| 152 | + } elseif ( $desktop_val !== '' ) { | |
| 153 | + $device_value = $desktop_val; | |
| 154 | + } else { | |
| 155 | + $device_value = $args['defaultValue']; | |
| 156 | + } | |
| 157 | + break; | |
| 158 | + | |
| 159 | + default: // Custom breakpoint suffix (e.g. "Bp1024") | |
| 160 | + $custom_val = self::get_filled_value( $value, $desktop_key . $args['device'] ); | |
| 161 | + if ( $custom_val !== '' ) { | |
| 162 | + $device_value = $custom_val; | |
| 163 | + } elseif ( $desktop_val !== '' ) { | |
| 164 | + $device_value = $desktop_val; | |
| 165 | + } else { | |
| 166 | + $device_value = $args['defaultValue']; | |
| 167 | + } | |
| 168 | + break; | |
| 169 | + }//end switch | |
| 170 | + | |
| 171 | + // ✅ If value exists, apply unit and return | |
| 172 | + if ( $device_value !== '' ) { | |
| 110 | 173 | $unit = self::get_unit( [ |
| 111 | 174 | 'attributeValue' => $value, |
| 112 | 175 | 'attributeObjectKey' => $args['attributeObjectKey'], |
| 113 | 176 | 'unitDefaultValue' => $args['unitDefaultValue'], |
| @@ -114,12 +177,12 @@ | ||
| 114 | 177 | 'device' => $args['device'], |
| 115 | 178 | ] ); |
| 116 | 179 | |
| 117 | 180 | if ( $args['property'] === 'value' ) { |
| 118 | - $css['value'] = $value[ $deviceValueKey ]; | |
| 181 | + $css['value'] = $device_value; | |
| 119 | 182 | $css['valueUnit'] = $unit; |
| 120 | 183 | } else { |
| 121 | - $css[ $args['property'] ] = $value[ $deviceValueKey ] . $unit; | |
| 184 | + $css[ $args['property'] ] = $device_value . $unit; | |
| 122 | 185 | } |
| 123 | 186 | } |
| 124 | 187 | |
| 125 | 188 | return $css; |
| @@ -124,5 +187,49 @@ | ||
| 124 | 187 | |
| 125 | 188 | return $css; |
| 126 | 189 | } |
| 127 | 190 | |
| 191 | + | |
| 192 | + public static function get_unit( $args ) { | |
| 193 | + $defaultUnit = $args['unitDefaultValue']; | |
| 194 | + $value = $args['attributeValue']; | |
| 195 | + $device = $args['device']; | |
| 196 | + $keyPrefix = $args['attributeObjectKey'] . 'Unit'; | |
| 197 | + | |
| 198 | + // Retrieve units with fallback to default | |
| 199 | + $unitDesktop = Helper::get_array_value( $value, $keyPrefix, $defaultUnit ); // Desktop | |
| 200 | + $unitTablet = Helper::get_array_value( $value, $keyPrefix . 'Tablet', $unitDesktop ); // Tablet inherits from Desktop | |
| 201 | + $unitMobile = Helper::get_array_value( $value, $keyPrefix . 'Mobile', $unitTablet ); // Mobile inherits from Tablet | |
| 202 | + | |
| 203 | + // Return the appropriate unit based on the device | |
| 204 | + if ( '' === $device ) { | |
| 205 | + return $unitDesktop; // Desktop | |
| 206 | + } | |
| 207 | + | |
| 208 | + if ( 'Tablet' === $device ) { | |
| 209 | + return $unitTablet; // Tablet | |
| 210 | + } | |
| 211 | + | |
| 212 | + if ( 'Mobile' === $device ) { | |
| 213 | + return $unitMobile; // Mobile | |
| 214 | + } | |
| 215 | + | |
| 216 | + if ( is_string( $device ) && '' !== $device ) { | |
| 217 | + // Custom breakpoint inherits the desktop unit when it has none of its own. | |
| 218 | + $unitCustom = self::get_filled_value( $value, $keyPrefix . $device ); | |
| 219 | + return '' !== $unitCustom ? $unitCustom : $unitDesktop; | |
| 220 | + } | |
| 221 | + | |
| 222 | + return $defaultUnit; // Default fallback | |
| 223 | + } | |
| 224 | + | |
| 225 | + /** | |
| 226 | + * A stored responsive member, or '' when it is missing, null or an empty | |
| 227 | + * string. Unlike empty(), a 0 value is kept. | |
| 228 | + */ | |
| 229 | + private static function get_filled_value( $value, $key ) { | |
| 230 | + if ( ! is_array( $value ) || ! isset( $value[ $key ] ) || is_array( $value[ $key ] ) || '' === (string) $value[ $key ] ) { | |
| 231 | + return ''; | |
| 232 | + } | |
| 233 | + return $value[ $key ]; | |
| 234 | + } | |
| 128 | 235 | } |