PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.5.6
Tableberg – Simple Gutenberg Table Block v0.5.6
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.6, at includes/Utils/Utils.php

430 lines 10.6 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($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 public static function get_border_radius_var($object, string $prefix, &$hasValue = false): array
201 {
202 if (is_string($object)) {
203 $hasValue = $object != '0px';
204 return [
205 $prefix . '-top-left' => $object,
206 $prefix . '-top-right' => $object,
207 $prefix . '-bottom-left' => $object,
208 $prefix . '-bottom-right' => $object,
209 ];
210 }
211
212 $css = [];
213
214 foreach ($object as $key => $value) {
215 $corner = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $key));
216 $css[$prefix . '-' . $corner] = $value;
217 if ($value != '0px') {
218 $hasValue = true;
219 }
220 }
221 return $css;
222 }
223
224 /**
225 * Get the CSS value for a single side of the border.
226 *
227 * @param array $border - border.
228 * @param string $side - border side.
229 * @return string CSS value for the specified side.
230 */
231 public static function get_single_side_border_value($border, $side)
232 {
233 $width = $border[$side]['width'] ?? '';
234 $style = $border[$side]['style'] ?? '';
235 $color = $border[$side]['color'] ?? '';
236
237 return "{$width} " . ($width && empty($border[$side]['style']) ? 'solid' : $style) . " {$color}";
238 }
239 /**
240 * Check if a value is considered empty based on certain conditions.
241 *
242 * @param mixed $value - The value to check.
243 * @return bool Whether the value is considered empty.
244 */
245 public static function is_value_empty($value)
246 {
247 return (
248 self::is_undefined($value) ||
249 false === $value ||
250 trim($value) === '' ||
251 trim($value) === 'undefined undefined undefined' ||
252 empty($value)
253 );
254 }
255 /**
256 * Get border variables for CSS.
257 *
258 * @param array $border - border.
259 * @param string $slug - slug to use in variable.
260 * @return array CSS styles for the border variables.
261 */
262 public static function get_border_variables_css($border, $slug)
263 {
264 $border_in_dimensions = self::get_border_css($border);
265 $border_sides = array('top', 'right', 'bottom', 'left');
266 $borders = array();
267
268 foreach ($border_sides as $side) {
269 $side_property = "--tableberg-{$slug}-border-{$side}";
270 $side_value = self::get_single_side_border_value($border_in_dimensions, $side);
271 $borders[$side_property] = $side_value;
272 }
273
274 return $borders;
275 }
276
277 /**
278 * Check if spacing value is preset or custom.
279 *
280 * @param string $value - spacing value.
281 * @return bool Whether the value is a preset or custom spacing.
282 */
283 public static function is_value_spacing_preset($value)
284 {
285 if (!$value || !is_string($value)) {
286 return false;
287 }
288 return '0' === $value || strpos($value, 'var:preset|spacing|') === 0;
289 }
290
291 /**
292 * Return the spacing variable for CSS.
293 *
294 * @param string $value - spacing value.
295 * @return string CSS variable or the original value.
296 */
297 public static function get_spacing_preset_css_var($value)
298 {
299 if (!$value) {
300 return null;
301 }
302
303 $matches = array();
304 preg_match('/var:preset\|spacing\|(.+)/', $value, $matches);
305
306 if (empty($matches)) {
307 return $value;
308 }
309 return "var(--wp--preset--spacing--{$matches[1]})";
310 }
311
312 /**
313 * Get the CSS for spacing.
314 *
315 * @param array $object - spacing object.
316 * @return array CSS styles for spacing.
317 */
318 public static function get_spacing_css($object)
319 {
320 $css = array();
321
322 foreach ($object as $key => $value) {
323 if (self::is_value_spacing_preset($value)) {
324 $css[$key] = self::get_spacing_preset_css_var($value);
325 } else {
326 $css[$key] = $value;
327 }
328 }
329
330 return $css;
331 }
332
333 /**
334 * Get the CSS for spacing.
335 *
336 * @param array $object - spacing object.
337 * @return array CSS styles for spacing.
338 */
339 public static function get_spacing_style(array $object, string $prop)
340 {
341 $css = [];
342
343 foreach ($object as $key => $value) {
344 if (self::is_value_spacing_preset($value)) {
345 $css[$prop . '-' . $key] = self::get_spacing_preset_css_var($value);
346 } else {
347 $css[$prop . '-' . $key] = $value;
348 }
349 }
350
351 return $css;
352 }
353
354
355 /**
356 * Get the CSS for spacing.
357 *
358 * @param string $value - spacing value.
359 * @return string CSS styles for spacing.
360 */
361 public static function get_spacing_css_single($value)
362 {
363 if (self::is_value_spacing_preset($value)) {
364 return self::get_spacing_preset_css_var($value);
365 } else {
366 return $value;
367 }
368 }
369
370 /**
371 * Check if a value is undefined.
372 *
373 * @param mixed $value - value.
374 * @return bool Whether the value is undefined.
375 */
376 public static function is_undefined($value)
377 {
378 return null === $value || !isset($value) || empty($value);
379 }
380
381 /**
382 * Generate CSS string if value is not empty.
383 *
384 * @param array $styles - CSS styles.
385 * @return string Generated CSS string.
386 */
387 public static function generate_css_string($styles)
388 {
389 $css_string = '';
390
391 foreach ($styles as $key => $value) {
392 if (!self::is_undefined($value) && false !== $value && trim($value) !== '' && trim($value) !== 'undefined undefined undefined' && !empty($value)) {
393 $css_string .= $key . ': ' . $value . '; ';
394 }
395 }
396
397 return esc_attr($css_string);
398 }
399
400 /**
401 * Get background color or gradient.
402 *
403 * @param array $attributes - block attributes.
404 * @param string $color_attr_key - block background color attribute key.
405 * @param string $gradient_attr_key - block background gradient attribute key.
406 * @return string Background color or gradient.
407 */
408 public static function get_background_color($attributes, $color_attr_key, $gradient_attr_key)
409 {
410 $bg_color = '';
411 if (!empty($attributes[$color_attr_key])) {
412 $bg_color = $attributes[$color_attr_key];
413 } elseif (!empty($attributes[$gradient_attr_key])) {
414 $bg_color = $attributes[$gradient_attr_key];
415 }
416 return $bg_color;
417 }
418
419
420 public static function get_any(array $attributes, string ...$keys)
421 {
422 foreach ($keys as $key) {
423 if (isset($attributes[$key]) && $attributes[$key]) {
424 return $attributes[$key];
425 }
426 }
427 return '';
428 }
429 }
430