PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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 / GutenbergBlock.php

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

78 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * GutenbergBlock class for handling IvyForms Gutenberg block registration
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\Exceptions\ValidationException;
13
14 /**
15 * Class GutenbergBlock
16 *
17 * @package IvyForms\Services\Blocks
18 */
19 class GutenbergBlock
20 {
21 /**
22 * Register the Gutenberg block
23 *
24 * @return void
25 */
26 public static function register(): void
27 {
28 if (!function_exists('register_block_type')) {
29 return;
30 }
31
32 // Register custom block category
33 // delegate category & assets registration to extracted helper
34 GutenbergBlockAssets::registerBlockCategory();
35 GutenbergBlockAssets::registerBlockAssets();
36
37 // Check if block.json exists for WordPress 5.8+ compatibility
38 $blockJsonPath = IVYFORMS_PATH . '/backend/src/Services/Integrations/Gutenberg/assets/block.json';
39
40 if (file_exists($blockJsonPath)) {
41 register_block_type($blockJsonPath, [
42 'render_callback' => [self::class, 'render'],
43 'editor_script' => 'ivyforms-gutenberg-block',
44 'editor_style' => 'ivyforms-gutenberg-block-editor',
45 ]);
46 return;
47 }
48
49 register_block_type('ivyforms/gutenberg-block', [
50 'render_callback' => [self::class, 'render'],
51 'attributes' => BlockAttributes::getAttributes(),
52 'api_version' => '2',
53 ]);
54 }
55
56 /**
57 * Render the Gutenberg block
58 *
59 * @param array<string, mixed> $attributes Block attributes
60 * @return string Rendered block HTML
61 * @throws ValidationException
62 */
63 public static function render(array $attributes): string
64 {
65 $formId = GutenbergBlockRenderer::getFormIdFromAttributes($attributes);
66 if ($formId === 0) {
67 return GutenbergBlockRenderer::renderNoFormSelected();
68 }
69
70 $form = GutenbergBlockRenderer::fetchPublishedForm($formId);
71 if ($form === null) {
72 return GutenbergBlockRenderer::renderFormNotFound();
73 }
74
75 return GutenbergBlockRenderer::renderFormBlock($formId, $attributes);
76 }
77 }
78