PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.5.1
Tableberg – Simple Gutenberg Table Block v0.5.1
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / includes / Utils / Utils.php

Utils.php in Tableberg – Simple Gutenberg Table Block 0.5.1, at includes/Utils/Utils.php

406 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Table Block
5 *
6 * @package Tableberg
7 */
8
9 namespace Tableberg\Utils;
10
11 /**
12 * Styling Help full functions utility class
13 */
14 class Utils
15 {
16
17 /**
18 * Get global font style variables for CSS.
19 * @return array CSS styles for the global font.
20 */
21 public static function get_global_style_variables_css(array $attributes)
22 {
23 return [
24 '--tableberg-global-text-color' => $attributes['fontColor'] ?? '',
25 '--tableberg-global-link-color' => $attributes['linkColor'] ?? '',
26 '--tableberg-global-font-size' => $attributes['fontSize'] ?? '',
27 ];
28 }
29
30 /**
31 * Get welcome page data.
32 *
33 * @return array
34 */
35 public static function welcome_page()
36 {
37 return array(
38 'welcome' => array(
39 'title' => 'Welcome to Tableberg!',
40 'content' => 'Elevate Your Content with Seamless Tables - The Ultimate WordPress Block Editor Plugin for Effortless Table Creation!',
41 ),
42 'documentation' => array(
43 'title' => 'Documentation',
44 'content' => 'Elevate your space with Tableberg: a sleek, modern table block for style and functionality. Crafted for timeless elegance and versatility.',
45 ),
46 'support' => array(
47 'title' => 'Support',
48 'content' => "Visit our Tableberg Support Page for quick solutions and assistance. We're here to ensure your Tableberg experience is seamless and satisfying.",
49 ),
50 'community' => array(
51 'title' => 'Join Community',
52 'content' => 'Join the vibrant Tableberg community. Connect, share, and discover endless possibilities together. Elevate your experience with like-minded enthusiasts now!',
53 ),
54 'upgrade' => array(
55 'title' => 'Upgrade to Tableberg PRO!',
56 'content' => 'Elevate Your Content with Seamless Tables - The Ultimate WordPress Block Editor Plugin for Effortless Table Creation!',
57 ),
58 );
59 }
60 /**
61 * Get Individual Control.
62 *
63 * @return array
64 */
65 public static function individual_control()
66 {
67 return array(
68 array(
69 'title' => 'Individual Control',
70 'name' => 'individual_control',
71 'content' => 'Elevate Your Content with Seamless Tables - The Ultimate WordPress Block Editor Plugin for Effortless Table Creation!',
72 'active' => false,
73 ),
74 );
75 }
76 /**
77 * Get global control.
78 *
79 * @return array
80 */
81 public static function global_control()
82 {
83 return array(
84 array(
85 'title' => 'Global Control',
86 'name' => 'global_control',
87 'content' => 'Elevate Your Content with Seamless Tables - The Ultimate WordPress Block Editor Plugin for Effortless Table Creation!',
88 'active' => true,
89 ),
90 );
91 }
92 /**
93 * Get default block properties.
94 *
95 * @return array
96 */
97 public static function default_block_properties()
98 {
99 return array(
100 array(
101 'title' => 'Default Row Number',
102 'name' => 'row_number',
103 'content' => 'Set your default row number of the table when you add Tableberg to your site. Further you can increase or decrease it there simply.',
104 'value' => 3,
105 ),
106 array(
107 'title' => 'Default Column Number',
108 'name' => 'column_number',
109 'content' => 'Set your default column number of the table when you add Tableberg to your site. Further you can increase or decrease it there simply.',
110 'value' => 3,
111 ),
112 array(
113 'title' => 'Default Property Font Size',
114 'name' => 'font_size',
115 'content' => 'Set your default FONT SIZE of the PROPERTY of the table when you add Tableberg to your site. Further you can increase or decrease it there simply.',
116 'value' => 18,
117 ),
118 );
119 }
120 /**
121 * Check if border has split borders.
122 *
123 * @param array $border - block border.
124 * @return bool Whether the border has split sides.
125 */
126 public static function has_split_borders($border = array())
127 {
128 $sides = array('top', 'right', 'bottom', 'left');
129 foreach ($border as $side => $value) {
130 if (in_array($side, $sides, true)) {
131 return true;
132 }
133 }
134
135 return false;
136 }
137
138 /**
139 * Get the border CSS from attributes.
140 *
141 * @param array $object - block border.
142 * @return array CSS styles for the border.
143 */
144 public static function get_border_css($object)
145 {
146 $css = array();
147
148 if (!self::has_split_borders($object)) {
149 $css['top'] = $object;
150 $css['right'] = $object;
151 $css['bottom'] = $object;
152 $css['left'] = $object;
153 return $css;
154 }
155
156 return $object;
157 }
158
159
160
161 public static function get_border_style($object): array
162 {
163
164 if (is_string($object)) {
165 return [
166 'border' => $object
167 ];
168 }
169 if (isset($object['color']) || isset($object['width'])) {
170 return [
171 'border-width' => $object['width'] ?? '',
172 'border-color' => $object['color'] ?? '',
173 ];
174 }
175 $css = [];
176
177 foreach ($object as $key => $value) {
178 $css['border-' . $key . '-width'] = $value['width'] ?? '';
179 $css['border-' . $key . '-color'] = $value['color'] ?? '';
180 }
181 return $css;
182 }
183 public static function get_border_radius_style(array $object): array
184 {
185 if (is_string($object)) {
186 return [
187 'border-radius' => $object
188 ];
189 }
190
191 $css = [];
192
193 foreach ($object as $key => $value) {
194 $corner = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $key));
195 $css['border-' . $corner . '-radius'] = $value;
196 }
197 return $css;
198 }
199
200 /**
201 * Get the CSS value for a single side of the border.
202 *
203 * @param array $border - border.
204 * @param string $side - border side.
205 * @return string CSS value for the specified side.
206 */
207 public static function get_single_side_border_value($border, $side)
208 {
209 $width = $border[$side]['width'] ?? '';
210 $style = $border[$side]['style'] ?? '';
211 $color = $border[$side]['color'] ?? '';
212
213 return "{$width} " . ($width && empty($border[$side]['style']) ? 'solid' : $style) . " {$color}";
214 }
215 /**
216 * Check if a value is considered empty based on certain conditions.
217 *
218 * @param mixed $value - The value to check.
219 * @return bool Whether the value is considered empty.
220 */
221 public static function is_value_empty($value)
222 {
223 return (
224 self::is_undefined($value) ||
225 false === $value ||
226 trim($value) === '' ||
227 trim($value) === 'undefined undefined undefined' ||
228 empty($value)
229 );
230 }
231 /**
232 * Get border variables for CSS.
233 *
234 * @param array $border - border.
235 * @param string $slug - slug to use in variable.
236 * @return array CSS styles for the border variables.
237 */
238 public static function get_border_variables_css($border, $slug)
239 {
240 $border_in_dimensions = self::get_border_css($border);
241 $border_sides = array('top', 'right', 'bottom', 'left');
242 $borders = array();
243
244 foreach ($border_sides as $side) {
245 $side_property = "--tableberg-{$slug}-border-{$side}";
246 $side_value = self::get_single_side_border_value($border_in_dimensions, $side);
247 $borders[$side_property] = $side_value;
248 }
249
250 return $borders;
251 }
252
253 /**
254 * Check if spacing value is preset or custom.
255 *
256 * @param string $value - spacing value.
257 * @return bool Whether the value is a preset or custom spacing.
258 */
259 public static function is_value_spacing_preset($value)
260 {
261 if (!$value || !is_string($value)) {
262 return false;
263 }
264 return '0' === $value || strpos($value, 'var:preset|spacing|') === 0;
265 }
266
267 /**
268 * Return the spacing variable for CSS.
269 *
270 * @param string $value - spacing value.
271 * @return string CSS variable or the original value.
272 */
273 public static function get_spacing_preset_css_var($value)
274 {
275 if (!$value) {
276 return null;
277 }
278
279 $matches = array();
280 preg_match('/var:preset\|spacing\|(.+)/', $value, $matches);
281
282 if (empty($matches)) {
283 return $value;
284 }
285 return "var(--wp--preset--spacing--{$matches[1]})";
286 }
287
288 /**
289 * Get the CSS for spacing.
290 *
291 * @param array $object - spacing object.
292 * @return array CSS styles for spacing.
293 */
294 public static function get_spacing_css($object)
295 {
296 $css = array();
297
298 foreach ($object as $key => $value) {
299 if (self::is_value_spacing_preset($value)) {
300 $css[$key] = self::get_spacing_preset_css_var($value);
301 } else {
302 $css[$key] = $value;
303 }
304 }
305
306 return $css;
307 }
308
309 /**
310 * Get the CSS for spacing.
311 *
312 * @param array $object - spacing object.
313 * @return array CSS styles for spacing.
314 */
315 public static function get_spacing_style(array $object, string $prop)
316 {
317 $css = [];
318
319 foreach ($object as $key => $value) {
320 if (self::is_value_spacing_preset($value)) {
321 $css[$prop . '-' . $key] = self::get_spacing_preset_css_var($value);
322 } else {
323 $css[$prop . '-' . $key] = $value;
324 }
325 }
326
327 return $css;
328 }
329
330
331 /**
332 * Get the CSS for spacing.
333 *
334 * @param string $value - spacing value.
335 * @return string CSS styles for spacing.
336 */
337 public static function get_spacing_css_single($value)
338 {
339 if (self::is_value_spacing_preset($value)) {
340 return self::get_spacing_preset_css_var($value);
341 } else {
342 return $value;
343 }
344 }
345
346 /**
347 * Check if a value is undefined.
348 *
349 * @param mixed $value - value.
350 * @return bool Whether the value is undefined.
351 */
352 public static function is_undefined($value)
353 {
354 return null === $value || !isset($value) || empty($value);
355 }
356
357 /**
358 * Generate CSS string if value is not empty.
359 *
360 * @param array $styles - CSS styles.
361 * @return string Generated CSS string.
362 */
363 public static function generate_css_string($styles)
364 {
365 $css_string = '';
366
367 foreach ($styles as $key => $value) {
368 if (!self::is_undefined($value) && false !== $value && trim($value) !== '' && trim($value) !== 'undefined undefined undefined' && !empty($value)) {
369 $css_string .= $key . ': ' . $value . '; ';
370 }
371 }
372
373 return $css_string;
374 }
375
376 /**
377 * Get background color or gradient.
378 *
379 * @param array $attributes - block attributes.
380 * @param string $color_attr_key - block background color attribute key.
381 * @param string $gradient_attr_key - block background gradient attribute key.
382 * @return string Background color or gradient.
383 */
384 public static function get_background_color($attributes, $color_attr_key, $gradient_attr_key)
385 {
386 $bg_color = '';
387 if (!empty($attributes[$color_attr_key])) {
388 $bg_color = $attributes[$color_attr_key];
389 } elseif (!empty($attributes[$gradient_attr_key])) {
390 $bg_color = $attributes[$gradient_attr_key];
391 }
392 return $bg_color;
393 }
394
395
396 public static function get_any(array $attributes, string ...$keys)
397 {
398 foreach ($keys as $key) {
399 if (isset($attributes[$key]) && $attributes[$key]) {
400 return $attributes[$key];
401 }
402 }
403 return '';
404 }
405 }
406