| 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 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class FormStyleService |
| 17 |
* |
| 18 |
* Generates scoped CSS from form style settings |
| 19 |
* |
| 20 |
* @package IvyForms\Services\Form |
| 21 |
*/ |
| 22 |
class FormStyleService |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Generate scoped CSS for a form |
| 26 |
* |
| 27 |
* @param int $formId |
| 28 |
* @param array<string, mixed>|null $styleSettings |
| 29 |
* @return string |
| 30 |
*/ |
| 31 |
public static function generateFormCSS(int $formId, ?array $styleSettings): string |
| 32 |
{ |
| 33 |
if (empty($styleSettings)) { |
| 34 |
return ''; |
| 35 |
} |
| 36 |
|
| 37 |
if (isset($styleSettings['stylesEnabled']) && $styleSettings['stylesEnabled'] === false) { |
| 38 |
return ''; |
| 39 |
} |
| 40 |
|
| 41 |
$css = new CssBuilder(); |
| 42 |
$context = self::buildContext($formId, $styleSettings); |
| 43 |
|
| 44 |
// Generate all CSS sections using factory |
| 45 |
$generators = StyleGeneratorFactory::createGenerators(); |
| 46 |
|
| 47 |
foreach ($generators as $generator) { |
| 48 |
$generator->generate($css, $context); |
| 49 |
} |
| 50 |
|
| 51 |
return $css->build(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Build the style context from form ID and settings |
| 56 |
* |
| 57 |
* @param int $formId |
| 58 |
* @param array<string, mixed> $styleSettings |
| 59 |
* @return StyleContext |
| 60 |
*/ |
| 61 |
private static function buildContext(int $formId, array $styleSettings): StyleContext |
| 62 |
{ |
| 63 |
$selectors = self::buildSelectors($formId, $styleSettings); |
| 64 |
$parser = new StyleSettingsParser($styleSettings); |
| 65 |
|
| 66 |
return new StyleContext( |
| 67 |
$selectors['base'], |
| 68 |
$selectors['innerWrapper'], |
| 69 |
$selectors['form'], |
| 70 |
$formId, |
| 71 |
$parser->getActiveColors(), |
| 72 |
$parser->getActiveStyles(), |
| 73 |
$styleSettings |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Build CSS selectors for the form |
| 79 |
* |
| 80 |
* @param int $formId |
| 81 |
* @param array<string, mixed> $styleSettings |
| 82 |
* @return array<string, string> |
| 83 |
*/ |
| 84 |
private static function buildSelectors(int $formId, array $styleSettings): array |
| 85 |
{ |
| 86 |
$formWrapperSelector = '.ivyforms-form-wrapper-' . $formId; |
| 87 |
$innerWrapperSelector = '.ivyforms-form-wrapper.ivyforms-form-wrapper-' . $formId; |
| 88 |
$formSelector = '.ivyforms-form-' . $formId; |
| 89 |
$customClass = !empty($styleSettings['customCssClass']) |
| 90 |
? '.' . $styleSettings['customCssClass'] |
| 91 |
: ''; |
| 92 |
|
| 93 |
$baseSelector = $formWrapperSelector; |
| 94 |
if ($customClass) { |
| 95 |
$baseSelector .= $customClass; |
| 96 |
} |
| 97 |
|
| 98 |
return [ |
| 99 |
'base' => $baseSelector, |
| 100 |
'innerWrapper' => $innerWrapperSelector, |
| 101 |
'form' => $formSelector, |
| 102 |
]; |
| 103 |
} |
| 104 |
} |
| 105 |
|