PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.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 All 81 releases
ablocks / includes / controls / range.php

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

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