| 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 ButtonStyleGenerator |
| 18 |
* |
| 19 |
* Generates CSS for form action buttons (submit row and multipage page navigation). |
| 20 |
* |
| 21 |
* @package IvyForms\Services\Form\StyleHelpers |
| 22 |
*/ |
| 23 |
class ButtonStyleGenerator implements StyleGeneratorInterface |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Generate button styles |
| 27 |
* |
| 28 |
* @param CssBuilder $css |
| 29 |
* @param StyleContext $context |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function generate(CssBuilder $css, StyleContext $context): void |
| 33 |
{ |
| 34 |
$activeColors = $context->getActiveColors(); |
| 35 |
$activeStyles = $context->getActiveStyles(); |
| 36 |
|
| 37 |
if (!$this->hasAnyButtonStyles($activeColors, $activeStyles)) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$selectors = $this->getButtonSelectors($context->getBaseSelector()); |
| 42 |
$styles = $this->buildButtonStyles($activeColors, $activeStyles); |
| 43 |
|
| 44 |
$css->addRule($selectors, $styles); |
| 45 |
$this->addHoverState($css, $selectors); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Build button styles |
| 50 |
* |
| 51 |
* @param array<string, string> $activeColors |
| 52 |
* @param array<string, string> $activeStyles |
| 53 |
* @return string[] |
| 54 |
*/ |
| 55 |
private function buildButtonStyles(array $activeColors, array $activeStyles): array |
| 56 |
{ |
| 57 |
$styles = []; |
| 58 |
|
| 59 |
if (!empty($activeColors['buttonBackground'])) { |
| 60 |
$styles[] = ' background-color: ' . esc_attr($activeColors['buttonBackground']) . ' !important;'; |
| 61 |
} |
| 62 |
|
| 63 |
if (!empty($activeColors['buttonText'])) { |
| 64 |
$styles[] = ' color: ' . esc_attr($activeColors['buttonText']) . ' !important;'; |
| 65 |
} |
| 66 |
|
| 67 |
$this->addButtonBorder($styles, $activeColors, $activeStyles); |
| 68 |
$this->addButtonRadius($styles, $activeStyles); |
| 69 |
$this->addButtonPadding($styles, $activeStyles); |
| 70 |
|
| 71 |
if (!empty($activeStyles['buttonWidth'])) { |
| 72 |
$styles[] = ' width: ' . esc_attr($activeStyles['buttonWidth']) . ' !important;'; |
| 73 |
} |
| 74 |
|
| 75 |
if (!empty($activeStyles['buttonHeight'])) { |
| 76 |
$styles[] = ' height: ' . esc_attr($activeStyles['buttonHeight']) . ' !important;'; |
| 77 |
} |
| 78 |
|
| 79 |
$styles[] = ' min-width: 4.5rem !important;'; |
| 80 |
$styles[] = ' min-height: 2.5rem !important;'; |
| 81 |
|
| 82 |
$styles[] = ' cursor: pointer !important;'; |
| 83 |
$styles[] = ' transition: all 0.2s ease !important;'; |
| 84 |
|
| 85 |
return $styles; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Add button padding |
| 90 |
* |
| 91 |
* @param string[] $styles |
| 92 |
* @param array<string, string> $activeStyles |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
private function addButtonPadding(array &$styles, array $activeStyles): void |
| 96 |
{ |
| 97 |
$paddingLeftRight = $activeStyles['buttonPaddingLeftRight'] ?? null; |
| 98 |
$paddingTopBottom = $activeStyles['buttonPaddingTopBottom'] ?? null; |
| 99 |
|
| 100 |
if ($paddingLeftRight || $paddingTopBottom) { |
| 101 |
$paddingLeftRight = $paddingLeftRight ?? '0px'; |
| 102 |
$paddingTopBottom = $paddingTopBottom ?? '0px'; |
| 103 |
$styles[] = ' padding: ' . esc_attr($paddingTopBottom) . ' ' . |
| 104 |
esc_attr($paddingLeftRight) . ' !important;'; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Add button border |
| 110 |
* |
| 111 |
* @param string[] $styles |
| 112 |
* @param array<string, string> $activeColors |
| 113 |
* @param array<string, string> $activeStyles |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
private function addButtonBorder( |
| 117 |
array &$styles, |
| 118 |
array $activeColors, |
| 119 |
array $activeStyles |
| 120 |
): void { |
| 121 |
$borderWidth = $activeStyles['buttonBorderWidth'] ?? null; |
| 122 |
|
| 123 |
if (empty($borderWidth) || $borderWidth === '0px') { |
| 124 |
$styles[] = ' border: none !important;'; |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
$borderColor = $activeColors['buttonBorderColor'] ?? $activeColors['buttonBackground'] ?? '#707883'; |
| 129 |
$styles[] = ' border: ' . esc_attr($borderWidth) . ' solid ' . esc_attr($borderColor) . ' !important;'; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Add button border radius |
| 134 |
* |
| 135 |
* @param string[] $styles |
| 136 |
* @param array<string, string> $activeStyles |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
private function addButtonRadius(array &$styles, array $activeStyles): void |
| 140 |
{ |
| 141 |
$radius = $activeStyles['buttonCornerRadius'] ?? '6px'; |
| 142 |
$styles[] = ' border-radius: ' . esc_attr($radius) . ' !important;'; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Add hover state |
| 147 |
* |
| 148 |
* @param CssBuilder $css |
| 149 |
* @param string[] $selectors |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
private function addHoverState(CssBuilder $css, array $selectors): void |
| 153 |
{ |
| 154 |
$hoverSelectors = array_map(fn($selector) => $selector . ':hover', $selectors); |
| 155 |
|
| 156 |
$css->addRule($hoverSelectors, [ |
| 157 |
' opacity: 0.9 !important;', |
| 158 |
' transform: translateY(-1px) !important;', |
| 159 |
]); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get button selectors |
| 164 |
* |
| 165 |
* @param string $baseSelector |
| 166 |
* @return string[] |
| 167 |
*/ |
| 168 |
private function getButtonSelectors(string $baseSelector): array |
| 169 |
{ |
| 170 |
return [ |
| 171 |
$baseSelector . ' .ivyforms-form-submit .ivyforms-button-action', |
| 172 |
$baseSelector . ' .ivyforms-form-submit button.ivyforms-button-action', |
| 173 |
$baseSelector . ' .ivyforms-form-page-navigation .ivyforms-button-action', |
| 174 |
$baseSelector . ' .ivyforms-form-page-navigation button.ivyforms-button-action', |
| 175 |
]; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Determine whether any button style option is active. |
| 180 |
* |
| 181 |
* @param array<string, string> $activeColors |
| 182 |
* @param array<string, string> $activeStyles |
| 183 |
* @return bool |
| 184 |
*/ |
| 185 |
private function hasAnyButtonStyles(array $activeColors, array $activeStyles): bool |
| 186 |
{ |
| 187 |
$colorKeys = [ |
| 188 |
'buttonBackground', |
| 189 |
'buttonText', |
| 190 |
'buttonBorderColor', |
| 191 |
]; |
| 192 |
|
| 193 |
foreach ($colorKeys as $key) { |
| 194 |
if (!empty($activeColors[$key])) { |
| 195 |
return true; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
$styleKeys = [ |
| 200 |
'buttonBorderWidth', |
| 201 |
'buttonPaddingLeftRight', |
| 202 |
'buttonPaddingTopBottom', |
| 203 |
'buttonWidth', |
| 204 |
'buttonHeight', |
| 205 |
'buttonCornerRadius', |
| 206 |
]; |
| 207 |
|
| 208 |
foreach ($styleKeys as $key) { |
| 209 |
if (!empty($activeStyles[$key])) { |
| 210 |
return true; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return false; |
| 215 |
} |
| 216 |
} |
| 217 |
|