| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\Form\Style; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; // Exit if accessed directly |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class FormWrapperStyleGenerator |
| 18 |
* |
| 19 |
* Generates CSS for form wrapper |
| 20 |
* |
| 21 |
* @package IvyForms\Services\Form\StyleHelpers |
| 22 |
*/ |
| 23 |
class FormWrapperStyleGenerator implements StyleGeneratorInterface |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @var string[] |
| 27 |
*/ |
| 28 |
private const ALLOWED_ALIGNMENTS = ['left', 'center', 'right']; |
| 29 |
|
| 30 |
/** |
| 31 |
* Generate form wrapper styles |
| 32 |
* |
| 33 |
* @param CssBuilder $css |
| 34 |
* @param StyleContext $context |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function generate(CssBuilder $css, StyleContext $context): void |
| 38 |
{ |
| 39 |
$wrapperStyles = $this->buildWrapperStyles( |
| 40 |
$context->getActiveColors(), |
| 41 |
$context->getActiveStyles(), |
| 42 |
$context->getStyleSettings() |
| 43 |
); |
| 44 |
|
| 45 |
if (!empty($wrapperStyles)) { |
| 46 |
$css->addRule($context->getBaseSelector(), $wrapperStyles); |
| 47 |
} |
| 48 |
|
| 49 |
$this->addInnerWrapperStyles($css, $context->getInnerWrapperSelector()); |
| 50 |
$this->addFormElementStyles( |
| 51 |
$css, |
| 52 |
$context->getBaseSelector(), |
| 53 |
$context->getFormSelector(), |
| 54 |
$context->getActiveStyles(), |
| 55 |
$context->getStyleSettings() |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Build wrapper styles array |
| 61 |
* |
| 62 |
* @param array<string, string> $activeColors |
| 63 |
* @param array<string, string> $activeStyles |
| 64 |
* @param array<string, mixed> $styleSettings |
| 65 |
* @return string[] |
| 66 |
*/ |
| 67 |
private function buildWrapperStyles( |
| 68 |
array $activeColors, |
| 69 |
array $activeStyles, |
| 70 |
array $styleSettings |
| 71 |
): array { |
| 72 |
$styles = []; |
| 73 |
|
| 74 |
if (!empty($activeColors['formBackground'])) { |
| 75 |
$styles[] = ' --ivyforms-color-form-background: ' |
| 76 |
. esc_attr($activeColors['formBackground']) . ';'; |
| 77 |
$styles[] = ' background-color: ' . esc_attr($activeColors['formBackground']) . ';'; |
| 78 |
} |
| 79 |
|
| 80 |
$hasFormPadding = array_key_exists('formPaddingTopBottom', $activeStyles) |
| 81 |
|| array_key_exists('formPaddingLeftRight', $activeStyles); |
| 82 |
|
| 83 |
if ($hasFormPadding) { |
| 84 |
$paddingTopBottom = $activeStyles['formPaddingTopBottom'] ?? '0px'; |
| 85 |
$paddingLeftRight = $activeStyles['formPaddingLeftRight'] ?? '0px'; |
| 86 |
$styles[] = ' padding: ' . esc_attr($paddingTopBottom) . ' ' . esc_attr($paddingLeftRight) . ';'; |
| 87 |
} |
| 88 |
|
| 89 |
// Add form alignment using flexbox |
| 90 |
$alignment = $this->getFormAlignment($styleSettings); |
| 91 |
if ('left' !== $alignment) { |
| 92 |
$styles[] = ' display: flex;'; |
| 93 |
$styles[] = ' flex-direction: column;'; |
| 94 |
$styles[] = ' align-items: ' . $this->mapAlignmentToFlexPosition($alignment) . ';'; |
| 95 |
} |
| 96 |
|
| 97 |
self::addBorderStyles($styles, $activeColors, $activeStyles); |
| 98 |
self::addCornerRadiusStyles($styles, $activeStyles); |
| 99 |
|
| 100 |
return $styles; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Add border styles |
| 105 |
* |
| 106 |
* @param string[] $styles |
| 107 |
* @param array<string, string> $activeColors |
| 108 |
* @param array<string, string> $activeStyles |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
private function addBorderStyles( |
| 112 |
array &$styles, |
| 113 |
array $activeColors, |
| 114 |
array $activeStyles |
| 115 |
): void { |
| 116 |
$hasBorder = !empty($activeColors['formBorderColor']) || !empty($activeStyles['formBorderWidth']); |
| 117 |
|
| 118 |
if (!$hasBorder) { |
| 119 |
$styles[] = ' border: none;'; |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if (!empty($activeColors['formBorderColor'])) { |
| 124 |
$styles[] = ' border-color: ' . esc_attr($activeColors['formBorderColor']) . ' !important;'; |
| 125 |
} |
| 126 |
|
| 127 |
if (!empty($activeStyles['formBorderWidth'])) { |
| 128 |
$styles[] = ' border-width: ' . esc_attr($activeStyles['formBorderWidth']) . ' !important;'; |
| 129 |
} |
| 130 |
|
| 131 |
$borderStyle = $activeStyles['formBorderStyle'] ?? 'solid'; |
| 132 |
$styles[] = ' border-style: ' . esc_attr($borderStyle) . ' !important;'; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Add corner radius styles |
| 137 |
* |
| 138 |
* @param string[] $styles |
| 139 |
* @param array<string, string> $activeStyles |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
private function addCornerRadiusStyles(array &$styles, array $activeStyles): void |
| 143 |
{ |
| 144 |
$radius = $activeStyles['formCornerRadius'] ?? null; |
| 145 |
|
| 146 |
if (!empty($radius)) { |
| 147 |
$styles[] = ' border-radius: ' . esc_attr($radius) . ' !important;'; |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$styles[] = ' border-radius: 0 !important;'; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Add inner wrapper styles to prevent double borders |
| 156 |
* |
| 157 |
* @param CssBuilder $css |
| 158 |
* @param string $innerWrapperSelector |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
private function addInnerWrapperStyles(CssBuilder $css, string $innerWrapperSelector): void |
| 162 |
{ |
| 163 |
$css->addRule($innerWrapperSelector, [ |
| 164 |
' border: none !important;', |
| 165 |
' box-shadow: none !important;', |
| 166 |
' background: transparent !important;', |
| 167 |
' padding: 0 !important;', |
| 168 |
]); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Add form element styles |
| 173 |
* |
| 174 |
* @param CssBuilder $css |
| 175 |
* @param string $baseSelector |
| 176 |
* @param string $formSelector |
| 177 |
* @param array<string, string> $activeStyles |
| 178 |
* @param array<string, mixed> $styleSettings |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
private function addFormElementStyles( |
| 182 |
CssBuilder $css, |
| 183 |
string $baseSelector, |
| 184 |
string $formSelector, |
| 185 |
array $activeStyles, |
| 186 |
array $styleSettings |
| 187 |
): void { |
| 188 |
$css->addRule($baseSelector . ' ' . $formSelector, [ |
| 189 |
' border: none !important;', |
| 190 |
' border-radius: 0 !important;', |
| 191 |
]); |
| 192 |
|
| 193 |
$formStyles = [ |
| 194 |
' border: none !important;', |
| 195 |
' box-shadow: none !important;', |
| 196 |
' background: transparent !important;', |
| 197 |
' display: flex !important;', |
| 198 |
' flex-direction: column !important;', |
| 199 |
]; |
| 200 |
|
| 201 |
// Add vertical and horizontal spacing via flexbox gap |
| 202 |
$verticalSpacing = $activeStyles['verticalSpacing'] ?? '16px'; |
| 203 |
$horizontalSpacing = $this->getHorizontalSpacing($styleSettings); |
| 204 |
|
| 205 |
// gap property expects: vertical horizontal |
| 206 |
$formStyles[] = ' gap: ' . esc_attr($verticalSpacing) . ' ' . esc_attr($horizontalSpacing) . ' !important;'; |
| 207 |
|
| 208 |
$css->addRule($baseSelector . ' form', $formStyles); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get form alignment from style settings |
| 213 |
* |
| 214 |
* @param array<string, mixed> $styleSettings |
| 215 |
* @return string |
| 216 |
*/ |
| 217 |
private function getFormAlignment(array $styleSettings): string |
| 218 |
{ |
| 219 |
$advancedMode = $styleSettings['advancedMode'] ?? []; |
| 220 |
$general = $advancedMode['general'] ?? []; |
| 221 |
$alignment = $general['alignment'] ?? 'left'; |
| 222 |
|
| 223 |
return in_array($alignment, self::ALLOWED_ALIGNMENTS, true) ? $alignment : 'left'; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Map alignment value to corresponding flex alignment value. |
| 228 |
* |
| 229 |
* @param string $alignment |
| 230 |
* @return string |
| 231 |
*/ |
| 232 |
private function mapAlignmentToFlexPosition(string $alignment): string |
| 233 |
{ |
| 234 |
if ('left' === $alignment) { |
| 235 |
return 'flex-start'; |
| 236 |
} |
| 237 |
|
| 238 |
return 'center' === $alignment ? 'center' : 'flex-end'; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get horizontal spacing from style settings |
| 243 |
* |
| 244 |
* @param array<string, mixed> $styleSettings |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
private function getHorizontalSpacing(array $styleSettings): string |
| 248 |
{ |
| 249 |
$advancedMode = $styleSettings['advancedMode'] ?? []; |
| 250 |
$general = $advancedMode['general'] ?? []; |
| 251 |
|
| 252 |
if (!empty($general['horizontalSpacing'])) { |
| 253 |
return $general['horizontalSpacing']; |
| 254 |
} |
| 255 |
|
| 256 |
$quickMode = $styleSettings['quickMode'] ?? []; |
| 257 |
return $quickMode['horizontalSpacing'] ?? '12px'; |
| 258 |
} |
| 259 |
} |
| 260 |
|