| @@ -1,0 +1,179 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Controls; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Helper; | |
| 9 | + | |
| 10 | +class Range { | |
| 11 | + | |
| 12 | + public static function get_attribute_default_value( $args ) { | |
| 13 | + | |
| 14 | + $unitObject = $args['hasUnit'] ? [ | |
| 15 | + $args['attributeObjectKey'] . 'Unit' => $args['unitDefaultValue'], | |
| 16 | + $args['attributeObjectKey'] . 'UnitTablet' => '', | |
| 17 | + $args['attributeObjectKey'] . 'UnitMobile' => '', | |
| 18 | + ] : []; | |
| 19 | + | |
| 20 | + if ( $args['isResponsive'] ) { | |
| 21 | + return array_merge([ | |
| 22 | + $args['attributeObjectKey'] => $args['defaultValue'], | |
| 23 | + $args['attributeObjectKey'] . 'Tablet' => $args['defaultValueTablet'], | |
| 24 | + $args['attributeObjectKey'] . 'Mobile' => $args['defaultValueMobile'] | |
| 25 | + ], $unitObject); | |
| 26 | + } elseif ( ! $args['isResponsive'] && $args['hasUnit'] ) { | |
| 27 | + return array_merge([ | |
| 28 | + $args['attributeObjectKey'] => $args['defaultValue'], | |
| 29 | + $args['attributeObjectKey'] . 'Unit' => $args['unitDefaultValue'] | |
| 30 | + ], $unitObject); | |
| 31 | + } | |
| 32 | + | |
| 33 | + return $args['defaultValue']; | |
| 34 | + } | |
| 35 | + | |
| 36 | + public static function get_attribute( $args ) { | |
| 37 | + $defaults = [ | |
| 38 | + 'attributeName' => '', | |
| 39 | + 'isResponsive' => false, | |
| 40 | + 'defaultValue' => '', | |
| 41 | + 'defaultValueTablet' => '', | |
| 42 | + 'defaultValueMobile' => '', | |
| 43 | + 'hasUnit' => false, | |
| 44 | + 'unitDefaultValue' => 'px', | |
| 45 | + 'attributeObjectKey' => 'value', | |
| 46 | + ]; | |
| 47 | + | |
| 48 | + $args = wp_parse_args( $args, $defaults ); | |
| 49 | + | |
| 50 | + if ( $args['isResponsive'] ) { | |
| 51 | + return [ | |
| 52 | + $args['attributeName'] => [ | |
| 53 | + 'type' => 'object', | |
| 54 | + 'default' => self::get_attribute_default_value( $args ) | |
| 55 | + ] | |
| 56 | + ]; | |
| 57 | + } elseif ( ! $args['isResponsive'] && $args['hasUnit'] ) { | |
| 58 | + return [ | |
| 59 | + $args['attributeName'] => [ | |
| 60 | + 'type' => 'object', | |
| 61 | + 'default' => self::get_attribute_default_value( $args ) | |
| 62 | + ] | |
| 63 | + ]; | |
| 64 | + } | |
| 65 | + | |
| 66 | + return [ | |
| 67 | + $args['attributeName'] => [ | |
| 68 | + 'type' => 'number', | |
| 69 | + 'default' => $args['defaultValue'] | |
| 70 | + ] | |
| 71 | + ]; | |
| 72 | + } | |
| 73 | + | |
| 74 | + public static function get_css( $args ) { | |
| 75 | + if ( ! isset( $args['property'] ) || empty( $args['property'] ) ) { | |
| 76 | + return []; | |
| 77 | + } | |
| 78 | + | |
| 79 | + // Set default values for missing arguments | |
| 80 | + $args = wp_parse_args( $args, [ | |
| 81 | + 'attributeValue' => '', | |
| 82 | + 'isResponsive' => false, | |
| 83 | + 'defaultValue' => '', | |
| 84 | + 'defaultValueTablet' => '', | |
| 85 | + 'defaultValueMobile' => '', | |
| 86 | + 'hasUnit' => false, | |
| 87 | + 'unitDefaultValue' => 'px', | |
| 88 | + 'attributeObjectKey' => 'value', | |
| 89 | + 'device' => '', | |
| 90 | + ]); | |
| 91 | + | |
| 92 | + $value = $args['attributeValue']; | |
| 93 | + $css = []; | |
| 94 | + | |
| 95 | + // ✅ Basic non-responsive + non-unit handling | |
| 96 | + if ( ! $args['isResponsive'] && ! $args['hasUnit'] ) { | |
| 97 | + if ( $args['property'] === 'value' ) { | |
| 98 | + $css['value'] = $value; | |
| 99 | + $css['valueUnit'] = $args['unitDefaultValue']; | |
| 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. | |
| 104 | + $css[ $args['property'] ] = $value . $args['unitDefaultValue']; | |
| 105 | + } | |
| 106 | + return $css; | |
| 107 | + } | |
| 108 | + | |
| 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 ) ) { | |
| 125 | + $unit = self::get_unit( [ | |
| 126 | + 'attributeValue' => $value, | |
| 127 | + 'attributeObjectKey' => $args['attributeObjectKey'], | |
| 128 | + 'unitDefaultValue' => $args['unitDefaultValue'], | |
| 129 | + 'device' => $args['device'], | |
| 130 | + ] ); | |
| 131 | + | |
| 132 | + if ( $args['property'] === 'value' ) { | |
| 133 | + $css['value'] = $device_value; | |
| 134 | + $css['valueUnit'] = $unit; | |
| 135 | + } else { | |
| 136 | + $css[ $args['property'] ] = $device_value . $unit; | |
| 137 | + } | |
| 138 | + } | |
| 139 | + | |
| 140 | + return $css; | |
| 141 | + } | |
| 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 | + } | |
| 179 | +} | |