| 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 |
// � |
| 96 |
Basic non-responsive + non-unit handling |
| 97 |
if ( ! $args['isResponsive'] && ! $args['hasUnit'] ) { |
| 98 |
if ( $args['property'] === 'value' ) { |
| 99 |
$css['value'] = $value; |
| 100 |
$css['valueUnit'] = $args['unitDefaultValue']; |
| 101 |
} elseif ( '' !== (string) $value ) { |
| 102 |
// Skip when there's no value — otherwise we emit the unit alone |
| 103 |
// (e.g. "transition-duration:s"), invalid CSS that bloats every |
| 104 |
// block's generated stylesheet. |
| 105 |
$css[ $args['property'] ] = $value . $args['unitDefaultValue']; |
| 106 |
} |
| 107 |
return $css; |
| 108 |
} |
| 109 |
|
| 110 |
// � |
| 111 |
Get values per device |
| 112 |
$desktop_key = $args['attributeObjectKey']; |
| 113 |
$tablet_key = $args['attributeObjectKey'] . 'Tablet'; |
| 114 |
$mobile_key = $args['attributeObjectKey'] . 'Mobile'; |
| 115 |
|
| 116 |
$desktop_val = ! empty( $value[ $desktop_key ] ) ? $value[ $desktop_key ] : ''; |
| 117 |
$tablet_val = ! empty( $value[ $tablet_key ] ) ? $value[ $tablet_key ] : ''; |
| 118 |
$mobile_val = ! empty( $value[ $mobile_key ] ) ? $value[ $mobile_key ] : ''; |
| 119 |
|
| 120 |
$device_value = ''; |
| 121 |
|
| 122 |
switch ( $args['device'] ) { |
| 123 |
case 'Mobile': |
| 124 |
if ( $mobile_val !== '' ) { |
| 125 |
$device_value = $mobile_val; |
| 126 |
} elseif ( $args['defaultValueMobile'] !== '' ) { |
| 127 |
$device_value = $args['defaultValueMobile']; |
| 128 |
} elseif ( $tablet_val !== '' ) { |
| 129 |
$device_value = $tablet_val; |
| 130 |
} elseif ( $args['defaultValueTablet'] !== '' ) { |
| 131 |
$device_value = $args['defaultValueTablet']; |
| 132 |
} elseif ( $desktop_val !== '' ) { |
| 133 |
$device_value = $desktop_val; |
| 134 |
} else { |
| 135 |
$device_value = $args['defaultValue']; |
| 136 |
} |
| 137 |
break; |
| 138 |
|
| 139 |
case 'Tablet': |
| 140 |
if ( $tablet_val !== '' ) { |
| 141 |
$device_value = $tablet_val; |
| 142 |
} elseif ( $args['defaultValueTablet'] !== '' ) { |
| 143 |
$device_value = $args['defaultValueTablet']; |
| 144 |
} elseif ( $desktop_val !== '' ) { |
| 145 |
$device_value = $desktop_val; |
| 146 |
} else { |
| 147 |
$device_value = $args['defaultValue']; |
| 148 |
} |
| 149 |
break; |
| 150 |
|
| 151 |
default: // Desktop |
| 152 |
if ( $desktop_val !== '' ) { |
| 153 |
$device_value = $desktop_val; |
| 154 |
} else { |
| 155 |
$device_value = $args['defaultValue']; |
| 156 |
} |
| 157 |
break; |
| 158 |
}//end switch |
| 159 |
|
| 160 |
// � |
| 161 |
If value exists, apply unit and return |
| 162 |
if ( $device_value !== '' ) { |
| 163 |
$unit = self::get_unit( [ |
| 164 |
'attributeValue' => $value, |
| 165 |
'attributeObjectKey' => $args['attributeObjectKey'], |
| 166 |
'unitDefaultValue' => $args['unitDefaultValue'], |
| 167 |
'device' => $args['device'], |
| 168 |
] ); |
| 169 |
|
| 170 |
if ( $args['property'] === 'value' ) { |
| 171 |
$css['value'] = $device_value; |
| 172 |
$css['valueUnit'] = $unit; |
| 173 |
} else { |
| 174 |
$css[ $args['property'] ] = $device_value . $unit; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return $css; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
public static function get_unit( $args ) { |
| 183 |
$defaultUnit = $args['unitDefaultValue']; |
| 184 |
$value = $args['attributeValue']; |
| 185 |
$device = $args['device']; |
| 186 |
$keyPrefix = $args['attributeObjectKey'] . 'Unit'; |
| 187 |
|
| 188 |
// Retrieve units with fallback to default |
| 189 |
$unitDesktop = Helper::get_array_value( $value, $keyPrefix, $defaultUnit ); // Desktop |
| 190 |
$unitTablet = Helper::get_array_value( $value, $keyPrefix . 'Tablet', $unitDesktop ); // Tablet inherits from Desktop |
| 191 |
$unitMobile = Helper::get_array_value( $value, $keyPrefix . 'Mobile', $unitTablet ); // Mobile inherits from Tablet |
| 192 |
|
| 193 |
// Return the appropriate unit based on the device |
| 194 |
if ( '' === $device ) { |
| 195 |
return $unitDesktop; // Desktop |
| 196 |
} |
| 197 |
|
| 198 |
if ( 'Tablet' === $device ) { |
| 199 |
return $unitTablet; // Tablet |
| 200 |
} |
| 201 |
|
| 202 |
if ( 'Mobile' === $device ) { |
| 203 |
return $unitMobile; // Mobile |
| 204 |
} |
| 205 |
|
| 206 |
return $defaultUnit; // Default fallback |
| 207 |
} |
| 208 |
} |
| 209 |
|