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 / GutenbergBlockRenderer.php

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

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