PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.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 / Integrations / Gutenberg / Blocks / GutenbergBlockRenderer.php

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

150 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * GutenbergBlockRenderer class for rendering IvyForms Gutenberg blocks
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\Helpers\FormPreviewHelper;
13 use IvyForms\Services\API\IvyFormsAPI;
14 use IvyForms\Services\Shortcode\ShortcodeService;
15 use IvyForms\Services\Translations\BackendStrings;
16
17 /**
18 * Class GutenbergBlockRenderer
19 *
20 * Handles rendering logic for the IvyForms Gutenberg block
21 *
22 * @package IvyForms\Services\Blocks
23 */
24 class GutenbergBlockRenderer
25 {
26 /**
27 * Determine if current request is an editor / REST preview request
28 *
29 * @return bool
30 */
31 public static function isEditorPreviewRequest(): bool
32 {
33 if (defined('REST_REQUEST') && REST_REQUEST) {
34 return true;
35 }
36
37 return function_exists('wp_is_json_request') && wp_is_json_request();
38 }
39
40 /**
41 * Extract formId from block attributes
42 *
43 * @param array<string, mixed> $attributes
44 * @return int
45 */
46 public static function getFormIdFromAttributes(array $attributes): int
47 {
48 return !empty($attributes['formId']) ? (int)$attributes['formId'] : 0;
49 }
50
51 /**
52 * Fetch the form and ensure it's published
53 *
54 * @param int $formId
55 * @return mixed|null Form object or null if not found/unpublished
56 */
57 public static function fetchPublishedForm(int $formId)
58 {
59 $form = IvyFormsAPI::getForm($formId);
60 if (is_wp_error($form) || !$form->isPublished()) {
61 return null;
62 }
63 return $form;
64 }
65
66 /**
67 * Render placeholder for no form selected
68 *
69 * @return string
70 */
71 public static function renderNoFormSelected(): string
72 {
73 $msg = BackendStrings::getCommonStrings()['please_select_form'];
74 return '<div class="ivyforms-no-form-selected"><p>' . esc_html($msg) . '</p></div>';
75 }
76
77 /**
78 * Render placeholder for form not found
79 *
80 * @return string
81 */
82 public static function renderFormNotFound(): string
83 {
84 $msg = BackendStrings::getCommonStrings()['form_not_found'];
85 return '<div class="ivyforms-form-not-found"><p>' . esc_html($msg) . '</p></div>';
86 }
87
88 /**
89 * Build shortcode arguments from block attributes
90 *
91 * @param int $formId
92 * @param array<string, mixed> $attributes
93 * @return array<string, mixed>
94 */
95 private static function buildShortcodeArgs(int $formId, array $attributes): array
96 {
97 $args = ['id' => $formId];
98
99 if (isset($attributes['showTitle'])) {
100 $args['show_title'] = $attributes['showTitle'] ? 'true' : 'false';
101 }
102
103 if (isset($attributes['showDescription'])) {
104 $args['show_description'] = $attributes['showDescription'] ? 'true' : 'false';
105 }
106
107 return $args;
108 }
109
110 /**
111 * Render the form block with all wrapper elements
112 *
113 * @param int $formId
114 * @param array<string, mixed> $attributes
115 * @return string
116 */
117 public static function renderFormBlock(int $formId, array $attributes): string
118 {
119 $blockInstanceId = 'ivyforms-block-' . wp_unique_id();
120 $isEditorPreview = self::isEditorPreviewRequest();
121
122 $wrapperAttributes = GutenbergBlockHtmlBuilder::buildWrapperAttributes($attributes, $blockInstanceId);
123 $formOutput = ShortcodeService::shortcodeHandler(self::buildShortcodeArgs($formId, $attributes));
124
125 if ($formOutput === '') {
126 return '';
127 }
128
129 if ($isEditorPreview) {
130 $wrapperAttributes['class'] .= ' ivyforms-block-editor-preview';
131 $formDataJson = FormPreviewHelper::generateFormDataJson($formId);
132 if ($formDataJson !== '') {
133 $wrapperAttributes['data-ivyforms-data'] = $formDataJson;
134 }
135 }
136
137 $wrapperAttrsString = GutenbergBlockHtmlBuilder::attributesToString($wrapperAttributes);
138 $customStyles = $isEditorPreview ? '' :
139 GutenbergBlockHtmlBuilder::buildCustomStyles($attributes, $blockInstanceId);
140 $embeddedScripts = $isEditorPreview ? FormPreviewHelper::generateJsonScripts($formId) : '';
141
142 return GutenbergBlockHtmlBuilder::buildBlockHtml(
143 $wrapperAttrsString,
144 $formOutput,
145 $customStyles,
146 $embeddedScripts
147 );
148 }
149 }
150