PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Services / Integrations / Gutenberg / Blocks / GutenbergBlockHtmlBuilder.php

GutenbergBlockHtmlBuilder.php in The Innovative Form Builder – IvyForms 0.8, at backend/src/Services/Integrations/Gutenberg/Blocks/GutenbergBlockHtmlBuilder.php

213 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * GutenbergBlockHtmlBuilder class for building block HTML output
5 *
6 * @copyright © TMS-Plugins. All rights reserved.
7 * @licence See LICENCE.md for license details.
8 */
9
10 namespace IvyForms\Services\Integrations\Gutenberg\Blocks;
11
12 // phpcs:disable PSR1.Files.SideEffects
13 if (!defined('ABSPATH')) {
14 exit; // Exit if accessed directly
15 }
16
17 use IvyForms\Services\Shortcode\ShortcodeService;
18
19 /**
20 * Class GutenbergBlockHtmlBuilder
21 *
22 * Handles HTML and style building for the IvyForms Gutenberg block
23 *
24 * @package IvyForms\Services\Blocks
25 */
26 class GutenbergBlockHtmlBuilder
27 {
28 /**
29 * Build wrapper attributes array
30 *
31 * @param array<string, mixed> $attributes
32 * @param string $blockInstanceId
33 * @return array<string, string>
34 */
35 public static function buildWrapperAttributes(array $attributes, string $blockInstanceId): array
36 {
37 $cssClasses = self::buildCssClasses($attributes);
38
39 return [
40 'id' => $blockInstanceId,
41 'class' => implode(' ', $cssClasses),
42 'data-show-title' => !empty($attributes['showTitle']) ? 'true' : 'false',
43 'data-show-description' => !empty($attributes['showDescription']) ? 'true' : 'false',
44 ];
45 }
46
47 /**
48 * Build CSS classes array from attributes
49 *
50 * @param array<string, mixed> $attributes
51 * @return array<int, string>
52 */
53 private static function buildCssClasses(array $attributes): array
54 {
55 $cssClasses = ['ivyforms-gutenberg-block'];
56
57 if (!empty($attributes['className'])) {
58 $cssClasses[] = sanitize_text_field($attributes['className']);
59 }
60
61 if (!empty($attributes['customCssClass'])) {
62 $cssClasses[] = sanitize_text_field($attributes['customCssClass']);
63 }
64
65 return $cssClasses;
66 }
67
68 /**
69 * Convert wrapper attributes array to HTML attribute string
70 *
71 * @param array<string, string> $wrapperAttributes
72 * @return string
73 */
74 public static function attributesToString(array $wrapperAttributes): string
75 {
76 $parts = [];
77 foreach ($wrapperAttributes as $key => $value) {
78 $parts[] = sprintf('%s="%s"', esc_attr($key), esc_attr($value));
79 }
80 return ' ' . implode(' ', $parts);
81 }
82
83 /**
84 * Build custom styles for hiding title/description
85 *
86 * @param array<string, mixed> $attributes
87 * @param string $blockInstanceId
88 * @return string
89 */
90 public static function buildCustomStyles(array $attributes, string $blockInstanceId): string
91 {
92 $rules = self::buildHideRules($attributes, $blockInstanceId);
93 return $rules !== '' ? '<style>' . $rules . '</style>' : '';
94 }
95
96 /**
97 * Build CSS rules for hiding elements
98 *
99 * @param array<string, mixed> $attributes
100 * @param string $blockInstanceId
101 * @return string
102 */
103 private static function buildHideRules(array $attributes, string $blockInstanceId): string
104 {
105 $selector = '#' . esc_attr($blockInstanceId);
106 $rules = [];
107
108 if (isset($attributes['showTitle']) && $attributes['showTitle'] === false) {
109 $rules[] = $selector . ' .ivyforms-form-title { display: none !important; }';
110 }
111
112 if (isset($attributes['showDescription']) && $attributes['showDescription'] === false) {
113 $rules[] = $selector . ' .ivyforms-form-description { display: none !important; }';
114 }
115
116 return implode('', $rules);
117 }
118
119 /**
120 * Generate embedded form data JSON for data attribute
121 *
122 * @param int $formId
123 * @return string
124 */
125 public static function generateFormDataJson(int $formId): string
126 {
127 if (empty(ShortcodeService::$formList)) {
128 return '';
129 }
130
131 $formDataMap = self::filterFormListById($formId);
132 if (empty($formDataMap)) {
133 return '';
134 }
135
136 $json = wp_json_encode($formDataMap);
137 return $json !== false ? $json : '';
138 }
139
140 /**
141 * Generate embedded JSON script blocks
142 *
143 * @param int $formId
144 * @return string
145 */
146 public static function generateJsonScripts(int $formId): string
147 {
148 if (empty(ShortcodeService::$formList)) {
149 return '';
150 }
151
152 $scripts = '';
153 foreach (ShortcodeService::$formList as $counterKey => $payload) {
154 if (isset($payload['id']) && (int)$payload['id'] === (int)$formId) {
155 $scripts .= self::buildScriptTag($counterKey, $payload);
156 }
157 }
158
159 return $scripts;
160 }
161
162 /**
163 * Build a single JSON script tag
164 *
165 * @param string $counterKey
166 * @param array<string, mixed> $payload
167 * @return string
168 */
169 private static function buildScriptTag(string $counterKey, array $payload): string
170 {
171 $json = wp_json_encode($payload);
172 $json = str_replace('</script>', '<\/script>', $json);
173 return '<script type="application/json" class="ivyforms-ssr-data"'
174 . ' data-ivyforms-counter="' . esc_attr($counterKey) . '">'
175 . $json . '</script>';
176 }
177
178 /**
179 * Filter form list by form ID
180 *
181 * @param int $formId
182 * @return array<string, mixed>
183 */
184 private static function filterFormListById(int $formId): array
185 {
186 $formDataMap = [];
187 foreach (ShortcodeService::$formList as $counterKey => $payload) {
188 if (isset($payload['id']) && (int)$payload['id'] === (int)$formId) {
189 $formDataMap[$counterKey] = $payload;
190 }
191 }
192 return $formDataMap;
193 }
194
195 /**
196 * Build the final block HTML
197 *
198 * @param string $wrapperAttrsString
199 * @param string $formOutput
200 * @param string $customStyles
201 * @param string $embeddedScripts
202 * @return string
203 */
204 public static function buildBlockHtml(
205 string $wrapperAttrsString,
206 string $formOutput,
207 string $customStyles,
208 string $embeddedScripts
209 ): string {
210 return $customStyles . '<div' . $wrapperAttrsString . '>' . $formOutput . '</div>' . $embeddedScripts;
211 }
212 }
213