PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.13.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.13.0
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / controls / range.php

range.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.13.0, at includes/controls/range.php

239 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // A stored 0 is a real value (e.g. "top: 0" at Tablet), so only a missing
117 // or empty-string key counts as unset — matching the editor helper.
118 $desktop_val = self::get_filled_value( $value, $desktop_key );
119 $tablet_val = self::get_filled_value( $value, $tablet_key );
120 $mobile_val = self::get_filled_value( $value, $mobile_key );
121
122 $device_value = '';
123
124 switch ( $args['device'] ) {
125 case '':
126 if ( $desktop_val !== '' ) {
127 $device_value = $desktop_val;
128 } else {
129 $device_value = $args['defaultValue'];
130 }
131 break;
132
133 case 'Mobile':
134 if ( $mobile_val !== '' ) {
135 $device_value = $mobile_val;
136 } elseif ( $args['defaultValueMobile'] !== '' ) {
137 $device_value = $args['defaultValueMobile'];
138 } elseif ( $tablet_val !== '' ) {
139 $device_value = $tablet_val;
140 } elseif ( $args['defaultValueTablet'] !== '' ) {
141 $device_value = $args['defaultValueTablet'];
142 } elseif ( $desktop_val !== '' ) {
143 $device_value = $desktop_val;
144 } else {
145 $device_value = $args['defaultValue'];
146 }
147 break;
148
149 case 'Tablet':
150 if ( $tablet_val !== '' ) {
151 $device_value = $tablet_val;
152 } elseif ( $args['defaultValueTablet'] !== '' ) {
153 $device_value = $args['defaultValueTablet'];
154 } elseif ( $desktop_val !== '' ) {
155 $device_value = $desktop_val;
156 } else {
157 $device_value = $args['defaultValue'];
158 }
159 break;
160
161 default: // Custom breakpoint suffix (e.g. "Bp1024")
162 $custom_val = self::get_filled_value( $value, $desktop_key . $args['device'] );
163 if ( $custom_val !== '' ) {
164 $device_value = $custom_val;
165 } elseif ( $desktop_val !== '' ) {
166 $device_value = $desktop_val;
167 } else {
168 $device_value = $args['defaultValue'];
169 }
170 break;
171 }//end switch
172
173 // �
174 If value exists, apply unit and return
175 if ( $device_value !== '' ) {
176 $unit = self::get_unit( [
177 'attributeValue' => $value,
178 'attributeObjectKey' => $args['attributeObjectKey'],
179 'unitDefaultValue' => $args['unitDefaultValue'],
180 'device' => $args['device'],
181 ] );
182
183 if ( $args['property'] === 'value' ) {
184 $css['value'] = $device_value;
185 $css['valueUnit'] = $unit;
186 } else {
187 $css[ $args['property'] ] = $device_value . $unit;
188 }
189 }
190
191 return $css;
192 }
193
194
195 public static function get_unit( $args ) {
196 $defaultUnit = $args['unitDefaultValue'];
197 $value = $args['attributeValue'];
198 $device = $args['device'];
199 $keyPrefix = $args['attributeObjectKey'] . 'Unit';
200
201 // Retrieve units with fallback to default
202 $unitDesktop = Helper::get_array_value( $value, $keyPrefix, $defaultUnit ); // Desktop
203 $unitTablet = Helper::get_array_value( $value, $keyPrefix . 'Tablet', $unitDesktop ); // Tablet inherits from Desktop
204 $unitMobile = Helper::get_array_value( $value, $keyPrefix . 'Mobile', $unitTablet ); // Mobile inherits from Tablet
205
206 // Return the appropriate unit based on the device
207 if ( '' === $device ) {
208 return $unitDesktop; // Desktop
209 }
210
211 if ( 'Tablet' === $device ) {
212 return $unitTablet; // Tablet
213 }
214
215 if ( 'Mobile' === $device ) {
216 return $unitMobile; // Mobile
217 }
218
219 if ( is_string( $device ) && '' !== $device ) {
220 // Custom breakpoint inherits the desktop unit when it has none of its own.
221 $unitCustom = self::get_filled_value( $value, $keyPrefix . $device );
222 return '' !== $unitCustom ? $unitCustom : $unitDesktop;
223 }
224
225 return $defaultUnit; // Default fallback
226 }
227
228 /**
229 * A stored responsive member, or '' when it is missing, null or an empty
230 * string. Unlike empty(), a 0 value is kept.
231 */
232 private static function get_filled_value( $value, $key ) {
233 if ( ! is_array( $value ) || ! isset( $value[ $key ] ) || is_array( $value[ $key ] ) || '' === (string) $value[ $key ] ) {
234 return '';
235 }
236 return $value[ $key ];
237 }
238 }
239