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

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

203 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * GutenbergBlockAssets class - extracted asset and editor registration logic
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 use IvyForms\Services\API\IvyFormsAPI;
14 use IvyForms\Services\Shortcode\ShortcodeService;
15
16 class GutenbergBlockAssets
17 {
18 /**
19 * Register custom block category for IvyForms
20 *
21 * @return void
22 */
23 public static function registerBlockCategory(): void
24 {
25 add_filter('block_categories_all', function ($categories) {
26 foreach ($categories as $category) {
27 if ($category['slug'] === 'ivyforms') {
28 return $categories;
29 }
30 }
31
32 // Add IvyForms category at the beginning
33 return array_merge(
34 [
35 [
36 'slug' => 'ivyforms',
37 'title' => __('IvyForms', 'ivyforms'),
38 'icon' => null,
39 ],
40 ],
41 $categories
42 );
43 });
44 }
45
46 /**
47 * Register block assets (JavaScript and CSS)
48 *
49 * @return void
50 */
51 public static function registerBlockAssets(): void
52 {
53 // Hook into enqueue_block_editor_assets for editor-only scripts (block registration)
54 add_action('enqueue_block_editor_assets', [self::class, 'enqueueBlockEditorAssetsCallback']);
55
56 // Hook into enqueue_block_assets for scripts that need to run in the iframe (Vue frontend)
57 add_action('enqueue_block_assets', [self::class, 'enqueueBlockAssetsCallback']);
58 }
59
60 /**
61 * Callback for enqueue_block_assets - runs in both editor and iframe
62 * This is where we load the Vue frontend scripts for the preview
63 *
64 * @return void
65 * @throws ValidationException
66 */
67 public static function enqueueBlockAssetsCallback(): void
68 {
69 // Only enqueue in admin/editor context
70 if (!is_admin()) {
71 return;
72 }
73
74 // Enqueue frontend Vue scripts for live preview in editor iframe
75 ShortcodeService::enqueueScripts();
76
77 // Add a small inline flag on the frontend script so the public bundle knows it's
78 // being loaded inside the Gutenberg editor
79 $frontendScriptHandle = IVYFORMS_DEV ?
80 'ivyforms_scripts_dev_vite' : 'ivyforms_script_index';
81 if (wp_script_is($frontendScriptHandle, 'registered') || wp_script_is($frontendScriptHandle, 'enqueued')) {
82 wp_add_inline_script(
83 $frontendScriptHandle,
84 "window.IvyForms = window.IvyForms || {}; window.IvyForms.isGutenbergEditor = true;",
85 'before'
86 );
87 }
88 }
89
90 /**
91 * Callback used for enqueueing block editor assets
92 *
93 * @return void
94 * @throws ValidationException
95 */
96 public static function enqueueBlockEditorAssetsCallback(): void
97 {
98 $assetFile = IVYFORMS_PATH . '/backend/src/Services/Integrations/Gutenberg/assets/build/index.asset.php';
99
100 // Default dependencies
101 $dependencies = [
102 'wp-blocks',
103 'wp-element',
104 'wp-components',
105 'wp-editor',
106 'wp-i18n',
107 'wp-block-editor',
108 'wp-server-side-render',
109 'wp-data',
110 ];
111 $version = IVYFORMS_VERSION;
112
113 if (file_exists($assetFile)) {
114 $asset = include $assetFile;
115 $dependencies = $asset['dependencies'] ?? $dependencies;
116 $version = $asset['version'] ?? $version;
117 }
118
119 // Enqueue block editor script
120 wp_enqueue_script(
121 'ivyforms-gutenberg-block',
122 IVYFORMS_URL . 'backend/src/Services/Integrations/Gutenberg/assets/build/index.js',
123 $dependencies,
124 $version,
125 false
126 );
127
128 // Initialize the global data object before our block script executes.
129 // We add this inline tied to our enqueued script so WP can manage ordering.
130 wp_add_inline_script(
131 'ivyforms-gutenberg-block',
132 'window.wpIvyFormDataList = window.wpIvyFormDataList || {};',
133 'before'
134 );
135
136 // Register and enqueue the admin preview helper script (handles SSR merges)
137 $adminPreviewSrc = IVYFORMS_URL . 'backend/src/Services/Integrations/Gutenberg/assets/admin-preview.js';
138 wp_register_script(
139 'ivyforms-admin-preview',
140 esc_url($adminPreviewSrc),
141 ['ivyforms-gutenberg-block'],
142 $version,
143 true
144 );
145 wp_enqueue_script('ivyforms-admin-preview');
146
147 // Get forms data and pass to JavaScript using wp_localize_script
148 $forms = self::getFormsForSelect();
149
150 // Localize script with forms data
151 wp_localize_script(
152 'ivyforms-gutenberg-block',
153 'ivyFormsGutenberg',
154 [
155 'forms' => $forms,
156 'adminUrl' => admin_url('admin.php?page=ivyforms-builder'),
157 ]
158 );
159
160 // Enqueue block editor styles
161 wp_enqueue_style(
162 'ivyforms-gutenberg-block-editor',
163 IVYFORMS_URL . 'backend/src/Services/Integrations/Gutenberg/assets/build/index.css',
164 ['wp-edit-blocks'],
165 $version
166 );
167 }
168
169 /**
170 * Get forms formatted for select dropdown
171 *
172 * @return array<int, array<string, mixed>> Array of forms with id, title, and settings
173 */
174 private static function getFormsForSelect(): array
175 {
176 $forms = IvyFormsAPI::getForms();
177
178 if (is_wp_error($forms) || empty($forms)) {
179 return [];
180 }
181
182 $formsList = [];
183 foreach ($forms as $form) {
184 if (is_object($form) && method_exists($form, 'getId') && method_exists($form, 'getName')) {
185 $formData = [
186 'value' => (string)$form->getId(),
187 'label' => $form->getName(),
188 ];
189 // Include form settings for syncing toggles in block editor
190 if (method_exists($form, 'isTitleVisible')) {
191 $formData['showTitle'] = $form->isTitleVisible();
192 }
193 if (method_exists($form, 'isDescriptionVisible')) {
194 $formData['showDescription'] = $form->isDescriptionVisible();
195 }
196 $formsList[] = $formData;
197 }
198 }
199
200 return $formsList;
201 }
202 }
203