ivyforms
/
backend
/
src
/
Services
/
Integrations
/
Gutenberg
/
Blocks
/
GutenbergBlockHtmlBuilder.php
GutenbergBlockHtmlBuilder.php in The Innovative Form Builder – IvyForms 1.1, at backend/src/Services/Integrations/Gutenberg/Blocks/GutenbergBlockHtmlBuilder.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * GutenbergBlockHtmlBuilder class for building block HTML output |
| 5 | * |
| 6 | * @copyright © Melograno Venture Studio. All rights reserved. |
| 7 | * @licence See LICENCE.md for license details. |
| 8 | */ |
| 9 | |
| 10 | namespace IvyForms\Services\Integrations\Gutenberg\Blocks; |
| 11 | |
| 12 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 13 | |
| 14 | /** |
| 15 | * Class GutenbergBlockHtmlBuilder |
| 16 | * |
| 17 | * Handles HTML and style building for the IvyForms Gutenberg block |
| 18 | * |
| 19 | * @package IvyForms\Services\Blocks |
| 20 | */ |
| 21 | class GutenbergBlockHtmlBuilder |
| 22 | { |
| 23 | /** |
| 24 | * Build wrapper attributes array |
| 25 | * |
| 26 | * @param array<string, mixed> $attributes |
| 27 | * @param string $blockInstanceId |
| 28 | * @return array<string, string> |
| 29 | */ |
| 30 | public static function buildWrapperAttributes(array $attributes, string $blockInstanceId): array |
| 31 | { |
| 32 | $cssClasses = self::buildCssClasses($attributes); |
| 33 | |
| 34 | return [ |
| 35 | 'id' => $blockInstanceId, |
| 36 | 'class' => implode(' ', $cssClasses), |
| 37 | 'data-show-title' => !empty($attributes['showTitle']) ? 'true' : 'false', |
| 38 | 'data-show-description' => !empty($attributes['showDescription']) ? 'true' : 'false', |
| 39 | ]; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Build CSS classes array from attributes |
| 44 | * |
| 45 | * @param array<string, mixed> $attributes |
| 46 | * @return array<int, string> |
| 47 | */ |
| 48 | private static function buildCssClasses(array $attributes): array |
| 49 | { |
| 50 | $cssClasses = ['ivyforms-gutenberg-block']; |
| 51 | |
| 52 | if (!empty($attributes['className'])) { |
| 53 | $cssClasses[] = Sanitizer::sanitizeCssClasses($attributes['className']); |
| 54 | } |
| 55 | |
| 56 | if (!empty($attributes['customCssClass'])) { |
| 57 | $cssClasses[] = Sanitizer::sanitizeCssClasses($attributes['customCssClass']); |
| 58 | } |
| 59 | |
| 60 | return $cssClasses; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Convert wrapper attributes array to HTML attribute string |
| 65 | * |
| 66 | * @param array<string, string> $wrapperAttributes |
| 67 | * @return string |
| 68 | */ |
| 69 | public static function attributesToString(array $wrapperAttributes): string |
| 70 | { |
| 71 | $parts = []; |
| 72 | foreach ($wrapperAttributes as $key => $value) { |
| 73 | $parts[] = sprintf('%s="%s"', esc_attr($key), esc_attr($value)); |
| 74 | } |
| 75 | return ' ' . implode(' ', $parts); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Build custom styles for hiding title/description |
| 80 | * |
| 81 | * @param array<string, mixed> $attributes |
| 82 | * @param string $blockInstanceId |
| 83 | * @return string |
| 84 | */ |
| 85 | public static function buildCustomStyles(array $attributes, string $blockInstanceId): string |
| 86 | { |
| 87 | $rules = self::buildHideRules($attributes, $blockInstanceId); |
| 88 | return $rules !== '' ? '<style>' . $rules . '</style>' : ''; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Build CSS rules for hiding elements |
| 93 | * |
| 94 | * @param array<string, mixed> $attributes |
| 95 | * @param string $blockInstanceId |
| 96 | * @return string |
| 97 | */ |
| 98 | private static function buildHideRules(array $attributes, string $blockInstanceId): string |
| 99 | { |
| 100 | $selector = '#' . esc_attr($blockInstanceId); |
| 101 | $rules = []; |
| 102 | |
| 103 | if (isset($attributes['showTitle']) && $attributes['showTitle'] === false) { |
| 104 | $rules[] = $selector . ' .ivyforms-form-title { display: none !important; }'; |
| 105 | } |
| 106 | |
| 107 | if (isset($attributes['showDescription']) && $attributes['showDescription'] === false) { |
| 108 | $rules[] = $selector . ' .ivyforms-form-description { display: none !important; }'; |
| 109 | } |
| 110 | |
| 111 | return implode('', $rules); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Build the final block HTML |
| 116 | * |
| 117 | * @param string $wrapperAttrsString |
| 118 | * @param string $formOutput |
| 119 | * @param string $customStyles |
| 120 | * @param string $embeddedScripts |
| 121 | * @return string |
| 122 | */ |
| 123 | public static function buildBlockHtml( |
| 124 | string $wrapperAttrsString, |
| 125 | string $formOutput, |
| 126 | string $customStyles, |
| 127 | string $embeddedScripts |
| 128 | ): string { |
| 129 | return $customStyles . '<div' . $wrapperAttrsString . '>' . $formOutput . '</div>' . $embeddedScripts; |
| 130 | } |
| 131 | } |
| 132 |