PluginProbe
The Innovative Form Builder – IvyForms / 1.1
The Innovative Form Builder – IvyForms v1.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Form / Style / ButtonStyleGenerator.php

ButtonStyleGenerator.php in The Innovative Form Builder – IvyForms 1.1, at backend/src/Services/Form/Style/ButtonStyleGenerator.php

214 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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[] = ' cursor: pointer !important;';
80 $styles[] = ' transition: all 0.2s ease !important;';
81
82 return $styles;
83 }
84
85 /**
86 * Add button padding
87 *
88 * @param string[] $styles
89 * @param array<string, string> $activeStyles
90 * @return void
91 */
92 private function addButtonPadding(array &$styles, array $activeStyles): void
93 {
94 $paddingLeftRight = $activeStyles['buttonPaddingLeftRight'] ?? null;
95 $paddingTopBottom = $activeStyles['buttonPaddingTopBottom'] ?? null;
96
97 if ($paddingLeftRight || $paddingTopBottom) {
98 $paddingLeftRight = $paddingLeftRight ?? '0px';
99 $paddingTopBottom = $paddingTopBottom ?? '0px';
100 $styles[] = ' padding: ' . esc_attr($paddingTopBottom) . ' ' .
101 esc_attr($paddingLeftRight) . ' !important;';
102 }
103 }
104
105 /**
106 * Add button border
107 *
108 * @param string[] $styles
109 * @param array<string, string> $activeColors
110 * @param array<string, string> $activeStyles
111 * @return void
112 */
113 private function addButtonBorder(
114 array &$styles,
115 array $activeColors,
116 array $activeStyles
117 ): void {
118 $borderWidth = $activeStyles['buttonBorderWidth'] ?? null;
119
120 if (empty($borderWidth) || $borderWidth === '0px') {
121 $styles[] = ' border: none !important;';
122 return;
123 }
124
125 $borderColor = $activeColors['buttonBorderColor'] ?? $activeColors['buttonBackground'] ?? '#707883';
126 $styles[] = ' border: ' . esc_attr($borderWidth) . ' solid ' . esc_attr($borderColor) . ' !important;';
127 }
128
129 /**
130 * Add button border radius
131 *
132 * @param string[] $styles
133 * @param array<string, string> $activeStyles
134 * @return void
135 */
136 private function addButtonRadius(array &$styles, array $activeStyles): void
137 {
138 $radius = $activeStyles['buttonCornerRadius'] ?? '6px';
139 $styles[] = ' border-radius: ' . esc_attr($radius) . ' !important;';
140 }
141
142 /**
143 * Add hover state
144 *
145 * @param CssBuilder $css
146 * @param string[] $selectors
147 * @return void
148 */
149 private function addHoverState(CssBuilder $css, array $selectors): void
150 {
151 $hoverSelectors = array_map(fn($selector) => $selector . ':hover', $selectors);
152
153 $css->addRule($hoverSelectors, [
154 ' opacity: 0.9 !important;',
155 ' transform: translateY(-1px) !important;',
156 ]);
157 }
158
159 /**
160 * Get button selectors
161 *
162 * @param string $baseSelector
163 * @return string[]
164 */
165 private function getButtonSelectors(string $baseSelector): array
166 {
167 return [
168 $baseSelector . ' .ivyforms-form-submit .ivyforms-button-action',
169 $baseSelector . ' .ivyforms-form-submit button.ivyforms-button-action',
170 $baseSelector . ' .ivyforms-form-page-navigation .ivyforms-button-action',
171 $baseSelector . ' .ivyforms-form-page-navigation button.ivyforms-button-action',
172 ];
173 }
174
175 /**
176 * Determine whether any button style option is active.
177 *
178 * @param array<string, string> $activeColors
179 * @param array<string, string> $activeStyles
180 * @return bool
181 */
182 private function hasAnyButtonStyles(array $activeColors, array $activeStyles): bool
183 {
184 $colorKeys = [
185 'buttonBackground',
186 'buttonText',
187 'buttonBorderColor',
188 ];
189
190 foreach ($colorKeys as $key) {
191 if (!empty($activeColors[$key])) {
192 return true;
193 }
194 }
195
196 $styleKeys = [
197 'buttonBorderWidth',
198 'buttonPaddingLeftRight',
199 'buttonPaddingTopBottom',
200 'buttonWidth',
201 'buttonHeight',
202 'buttonCornerRadius',
203 ];
204
205 foreach ($styleKeys as $key) {
206 if (!empty($activeStyles[$key])) {
207 return true;
208 }
209 }
210
211 return false;
212 }
213 }
214