| @@ -1,0 +1,235 @@ | ||
| 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 | + // ✅ 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 !== '' ) { | |
| 173 | + $unit = self::get_unit( [ | |
| 174 | + 'attributeValue' => $value, | |
| 175 | + 'attributeObjectKey' => $args['attributeObjectKey'], | |
| 176 | + 'unitDefaultValue' => $args['unitDefaultValue'], | |
| 177 | + 'device' => $args['device'], | |
| 178 | + ] ); | |
| 179 | + | |
| 180 | + if ( $args['property'] === 'value' ) { | |
| 181 | + $css['value'] = $device_value; | |
| 182 | + $css['valueUnit'] = $unit; | |
| 183 | + } else { | |
| 184 | + $css[ $args['property'] ] = $device_value . $unit; | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + return $css; | |
| 189 | + } | |
| 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 | + } | |
| 235 | +} | |