| @@ -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,40 @@ | ||
| 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 | + // ✅ Resolve value by device and inheritance: the device's own value, then | |
| 110 | + // its declared default (defaultValue / defaultValueTablet / …Mobile), then | |
| 111 | + // the same for each containing wider device — custom breakpoints included. | |
| 112 | + // Mirrors getCSS() in src/controls/range/helper.js. | |
| 113 | + $device_defaults = [ | |
| 114 | + '' => $args['defaultValue'], | |
| 115 | + 'Tablet' => $args['defaultValueTablet'], | |
| 116 | + 'Mobile' => $args['defaultValueMobile'], | |
| 117 | + ]; | |
| 118 | + $device_value = self::resolve( $args['device'], function ( $suffix ) use ( $value, $args, $device_defaults ) { | |
| 119 | + $stored = self::get_filled_value( $value, $args['attributeObjectKey'] . $suffix ); | |
| 120 | + return Helper::has_responsive_value( $stored ) ? $stored : ( $device_defaults[ $suffix ] ?? '' ); | |
| 121 | + } ); | |
| 122 | + | |
| 123 | + // ✅ If value exists, apply unit and return | |
| 124 | + if ( Helper::has_responsive_value( $device_value ) ) { | |
| 110 | 125 | $unit = self::get_unit( [ |
| 111 | 126 | 'attributeValue' => $value, |
| 112 | 127 | 'attributeObjectKey' => $args['attributeObjectKey'], |
| 113 | 128 | 'unitDefaultValue' => $args['unitDefaultValue'], |
| @@ -114,12 +129,12 @@ | ||
| 114 | 129 | 'device' => $args['device'], |
| 115 | 130 | ] ); |
| 116 | 131 | |
| 117 | 132 | if ( $args['property'] === 'value' ) { |
| 118 | - $css['value'] = $value[ $deviceValueKey ]; | |
| 133 | + $css['value'] = $device_value; | |
| 119 | 134 | $css['valueUnit'] = $unit; |
| 120 | 135 | } else { |
| 121 | - $css[ $args['property'] ] = $value[ $deviceValueKey ] . $unit; | |
| 136 | + $css[ $args['property'] ] = $device_value . $unit; | |
| 122 | 137 | } |
| 123 | 138 | } |
| 124 | 139 | |
| 125 | 140 | return $css; |
| @@ -124,5 +139,41 @@ | ||
| 124 | 139 | |
| 125 | 140 | return $css; |
| 126 | 141 | } |
| 127 | 142 | |
| 143 | + | |
| 144 | + public static function get_unit( $args ) { | |
| 145 | + $key_prefix = $args['attributeObjectKey'] . 'Unit'; | |
| 146 | + $value = $args['attributeValue']; | |
| 147 | + $unit = self::resolve( $args['device'], function ( $suffix ) use ( $value, $key_prefix ) { | |
| 148 | + return self::get_filled_value( $value, $key_prefix . $suffix ); | |
| 149 | + } ); | |
| 150 | + return Helper::has_responsive_value( $unit ) ? $unit : $args['unitDefaultValue']; | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * The device's own value, else the nearest containing wider device's (see | |
| 155 | + * Helper::get_responsive_ancestors()), as read by $read_at( $suffix ). | |
| 156 | + * Returns null when nothing is set. | |
| 157 | + */ | |
| 158 | + private static function resolve( $device, $read_at ) { | |
| 159 | + $target = 'Desktop' === $device ? '' : (string) $device; | |
| 160 | + foreach ( array_merge( [ $target ], Helper::get_responsive_ancestors( $target ) ) as $suffix ) { | |
| 161 | + $v = $read_at( $suffix ); | |
| 162 | + if ( Helper::has_responsive_value( $v ) ) { | |
| 163 | + return $v; | |
| 164 | + } | |
| 165 | + } | |
| 166 | + return null; | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * A stored responsive member, or '' when it is missing, null or an empty | |
| 171 | + * string. Unlike empty(), a 0 value is kept. | |
| 172 | + */ | |
| 173 | + private static function get_filled_value( $value, $key ) { | |
| 174 | + if ( ! is_array( $value ) || ! isset( $value[ $key ] ) || is_array( $value[ $key ] ) || '' === (string) $value[ $key ] ) { | |
| 175 | + return ''; | |
| 176 | + } | |
| 177 | + return $value[ $key ]; | |
| 178 | + } | |
| 128 | 179 | } |