| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.2.6 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Helpers\Controls; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Spacing |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Default Spacing Control Positions. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected static $spacing_positions = ['top', 'right', 'bottom', 'left']; |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the CSS of the spacing control. |
| 30 |
* |
| 31 |
* @param array $value |
| 32 |
* @param string $prefix |
| 33 |
* @param string $breakpoint |
| 34 |
* @param string $unit |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public static function getResponsiveSpacingControlValue($value, $prefix = '', $unit = '', $breakpoint = '') |
| 39 |
{ |
| 40 |
$value = self::prepareSpacingControlValue($value, 'desktop'); |
| 41 |
|
| 42 |
if (is_null($value)) |
| 43 |
{ |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// Return the value for a specific breakpoint |
| 48 |
if (!empty($breakpoint) && is_string($breakpoint)) |
| 49 |
{ |
| 50 |
if (!isset($value[$breakpoint])) |
| 51 |
{ |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if (!is_array($value[$breakpoint]) && (string) $value[$breakpoint] !== '0') |
| 56 |
{ |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
return $prefix . ': ' . self::getSpacingValue($value[$breakpoint], $unit) . ';'; |
| 61 |
} |
| 62 |
|
| 63 |
// Return the value for all breakpoints |
| 64 |
$css = []; |
| 65 |
|
| 66 |
if (is_array($value)) |
| 67 |
{ |
| 68 |
unset($value['linked']); |
| 69 |
} |
| 70 |
|
| 71 |
foreach ($value as $_breakpoint => $values) |
| 72 |
{ |
| 73 |
// remove linked property |
| 74 |
if (isset($values['linked'])) |
| 75 |
{ |
| 76 |
unset($values['linked']); |
| 77 |
} |
| 78 |
|
| 79 |
if (!$value = self::getSpacingValue($values, $unit)) |
| 80 |
{ |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
$css[$_breakpoint] = $prefix . ': ' . $value . ';'; |
| 85 |
} |
| 86 |
|
| 87 |
return $css; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Prepares the value |
| 92 |
* |
| 93 |
* @param mixed $value |
| 94 |
* @param string $breakpoint |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public static function prepareSpacingControlValue($value, $breakpoint = 'desktop') |
| 99 |
{ |
| 100 |
if (is_null($value)) |
| 101 |
{ |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
if (!is_array($value)) |
| 106 |
{ |
| 107 |
$new_value = []; |
| 108 |
foreach (static::$spacing_positions as $pos) |
| 109 |
{ |
| 110 |
$new_value[$pos] = $value; |
| 111 |
} |
| 112 |
|
| 113 |
if (!empty($breakpoint) && is_string($breakpoint)) |
| 114 |
{ |
| 115 |
return [ |
| 116 |
$breakpoint => $new_value |
| 117 |
]; |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
return $new_value; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// If no breakpoint exists in the given value, set it to the given $breakpoint. |
| 126 |
if ((!isset($value['desktop']) && !isset($value['tablet']) && !isset($value['mobile'])) && ($breakpoint && !isset($value[$breakpoint]))) |
| 127 |
{ |
| 128 |
return [$breakpoint => $value]; |
| 129 |
} |
| 130 |
|
| 131 |
return $value; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Returns the value of a spacing control (margin, padding). |
| 136 |
* |
| 137 |
* @param array $value |
| 138 |
* @param string $unit |
| 139 |
* |
| 140 |
* @return string |
| 141 |
*/ |
| 142 |
public static function getSpacingValue($value, $unit = '') |
| 143 |
{ |
| 144 |
if (!is_string($unit)) |
| 145 |
{ |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if (is_string($value) && $value === '') |
| 150 |
{ |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
if (!$value || !is_array($value)) |
| 155 |
{ |
| 156 |
$value = self::prepareSpacingControlValue($value, false); |
| 157 |
} |
| 158 |
|
| 159 |
// If its a multi-dimensional array, return |
| 160 |
if (count($value) !== count($value, COUNT_RECURSIVE)) |
| 161 |
{ |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
try { |
| 166 |
// If all values are empty, return |
| 167 |
if (empty(array_filter($value, static function ($item) { |
| 168 |
if (is_scalar($item) || $item === null) |
| 169 |
{ |
| 170 |
return strlen((string) $item) > 0; |
| 171 |
} |
| 172 |
|
| 173 |
return false; |
| 174 |
}))) |
| 175 |
{ |
| 176 |
return; |
| 177 |
} |
| 178 |
} |
| 179 |
catch (\Exception $ex) |
| 180 |
{ |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$return = []; |
| 185 |
|
| 186 |
foreach (static::$spacing_positions as $pos) |
| 187 |
{ |
| 188 |
$return[$pos] = isset($value[$pos]) && $value[$pos] !== '' ? $value[$pos] : 0; |
| 189 |
} |
| 190 |
|
| 191 |
if (empty($return)) |
| 192 |
{ |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* All values are duplicates, return only 1 number with their unit. |
| 198 |
* |
| 199 |
* Example: Given [5, 5, 5, 5] to print the margin in pixels, do not return `margin: 5px 5px 5px 5px`. |
| 200 |
* Rather return `margin: 5px` |
| 201 |
*/ |
| 202 |
if (count(array_unique($return)) === 1) |
| 203 |
{ |
| 204 |
return reset($return) . $unit; |
| 205 |
} |
| 206 |
|
| 207 |
// add unit suffix |
| 208 |
$return = preg_filter('/$/', $unit, $return); |
| 209 |
|
| 210 |
return implode(' ', $return); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Checks whether the spacing value is empty. |
| 215 |
* |
| 216 |
* @param array $value |
| 217 |
* |
| 218 |
* @return boolean |
| 219 |
*/ |
| 220 |
public static function isEmpty($value) |
| 221 |
{ |
| 222 |
if (!is_array($value)) |
| 223 |
{ |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
foreach (static::$spacing_positions as $pos) |
| 228 |
{ |
| 229 |
if (!isset($value[$pos]) || (empty($value[$pos]) && (string) $value[$pos] !== '0')) |
| 230 |
{ |
| 231 |
continue; |
| 232 |
} |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
return true; |
| 238 |
} |
| 239 |
} |
| 240 |
|