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

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

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