@forgewp/compiler 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/README.md +70 -3
  2. package/bin/add.js +30 -15
  3. package/bin/analyze.js +66 -1
  4. package/bin/cli.js +149 -70
  5. package/bin/doctor.js +21 -12
  6. package/bin/export.js +69 -20
  7. package/bin/fresh.js +6 -1
  8. package/bin/i18n-extract.js +3 -10
  9. package/bin/i18n-translate.js +15 -24
  10. package/bin/make-block.js +24 -0
  11. package/bin/make-editable.js +166 -0
  12. package/bin/make-form.js +131 -0
  13. package/bin/make-island.js +11 -0
  14. package/bin/make-loop.js +147 -0
  15. package/bin/make-mail.js +27 -0
  16. package/bin/make-page.js +83 -0
  17. package/bin/make-post-type.js +68 -29
  18. package/bin/make-sandbox.js +609 -0
  19. package/bin/make-section.js +221 -0
  20. package/bin/make-shell.js +375 -0
  21. package/bin/repair.js +14 -26
  22. package/bin/seed.js +119 -0
  23. package/bin/sync-routes.js +107 -85
  24. package/lib/adapters/html-adapter.js +13 -3
  25. package/lib/adapters/stub-adapter.js +10 -2
  26. package/lib/add-component.js +153 -92
  27. package/lib/asset-loader.js +333 -1
  28. package/lib/assets/asset-graph.js +432 -0
  29. package/lib/assets/asset-pipeline.js +125 -0
  30. package/lib/assets/diagnostics-report.js +345 -0
  31. package/lib/assets/external-fetcher.js +173 -0
  32. package/lib/assets/font-orchestrator.js +348 -0
  33. package/lib/assets/font-usage-scanner.js +242 -0
  34. package/lib/assets/image-optimizer.js +229 -0
  35. package/lib/assets/index.js +51 -0
  36. package/lib/assets/preload-generator.js +70 -0
  37. package/lib/assets/public-url.js +153 -0
  38. package/lib/block-authoring-help.js +11 -0
  39. package/lib/blocks/ast-parser.js +67 -0
  40. package/lib/blocks/discover-blocks.js +146 -0
  41. package/lib/blocks/editor-scope.js +733 -0
  42. package/lib/blocks/editor-transpiler.js +1229 -0
  43. package/lib/blocks/index.js +2838 -0
  44. package/lib/blocks/php-transpiler.js +4928 -0
  45. package/lib/blocks/scanner.js +1487 -0
  46. package/lib/blocks/shared-utils.js +290 -0
  47. package/lib/blocks/source-sanitize.js +1449 -0
  48. package/lib/blueprints.js +77 -5
  49. package/lib/build-assets.js +38 -33
  50. package/lib/collect-page-template-routes.js +102 -0
  51. package/lib/collect-static-routes.js +175 -0
  52. package/lib/diagnostics.js +58 -33
  53. package/lib/export-theme.js +84 -5
  54. package/lib/extract-template-content.js +90 -0
  55. package/lib/forms-preview.js +42 -0
  56. package/lib/functions/cpt-routing.js +69 -0
  57. package/lib/functions/forms.js +2461 -0
  58. package/lib/functions/hydration-enqueuer.js +400 -0
  59. package/lib/functions/index.js +1623 -0
  60. package/lib/functions/load-menus.js +112 -0
  61. package/lib/functions/load-translations.js +148 -0
  62. package/lib/functions/rest-endpoints.js +2923 -0
  63. package/lib/functions/routing.js +765 -0
  64. package/lib/functions/seed-mock-data.js +392 -0
  65. package/lib/functions/seed-products.js +481 -0
  66. package/lib/functions/settings-page.js +273 -0
  67. package/lib/functions/theme-setup.js +494 -0
  68. package/lib/generate-theme.js +2076 -303
  69. package/lib/hydration/editable-schemas.js +332 -0
  70. package/lib/hydration/enter-hidden-initial.js +65 -0
  71. package/lib/hydration/form-schemas.js +227 -0
  72. package/lib/hydration/index.js +12 -0
  73. package/lib/hydration/is-interactive.js +951 -0
  74. package/lib/hydration/island-split.js +1231 -0
  75. package/lib/hydration/islands-scanner.js +599 -0
  76. package/lib/hydration/resolve-island-chunk.js +103 -0
  77. package/lib/hydration/resolve-island-component.js +52 -0
  78. package/lib/hydration/rollup-inputs.js +81 -0
  79. package/lib/hydration/root-class-extractor.js +220 -0
  80. package/lib/hydration/scan-app-providers.js +154 -0
  81. package/lib/hydration/static-lint.js +382 -0
  82. package/lib/hydration/transform.js +257 -0
  83. package/lib/hydration/uses-block-area.js +49 -0
  84. package/lib/hydration/utils.js +119 -0
  85. package/lib/i18n/translation-cache.js +141 -0
  86. package/lib/icon-registry.js +72 -0
  87. package/lib/index.d.ts +324 -8
  88. package/lib/index.js +3 -2
  89. package/lib/load-config.js +338 -144
  90. package/lib/mail-templates.js +37 -0
  91. package/lib/markup-processor.js +483 -92
  92. package/lib/node-runtime.js +7 -0
  93. package/lib/page-config-plugin.js +84 -0
  94. package/lib/php-builders.js +383 -42
  95. package/lib/react-adapter.js +1053 -246
  96. package/lib/registration-metadata.js +11 -0
  97. package/lib/render-theme.mts +1410 -543
  98. package/lib/route-layout.js +76 -0
  99. package/lib/template-discovery.js +58 -0
  100. package/lib/theme-compatibility.js +32 -0
  101. package/lib/validate-export.js +51 -2
  102. package/lib/validate.js +27 -5
  103. package/lib/virtual-plugin.js +319 -0
  104. package/lib/walkthrough.md +26 -0
  105. package/package.json +17 -5
  106. package/templates/PresetsStyle.tsx +1 -1
  107. package/templates/auth/cms/email-logs.json +9 -0
  108. package/templates/auth/cms/menus.json +11 -0
  109. package/templates/auth/cms/mock-data.json +123 -0
  110. package/templates/auth/cms/roles.json +7 -0
  111. package/templates/auth/cms/sessions.json +1 -0
  112. package/templates/auth/cms/site-settings.json +28 -0
  113. package/templates/auth/cms/translations.json +4 -0
  114. package/templates/auth/cms/users.json +46 -0
  115. package/templates/auth/components/dashboard-view-wrapper.tsx +168 -0
  116. package/templates/auth/components/forgot-password-form-wrapper.tsx +141 -0
  117. package/templates/auth/components/login-form-wrapper.tsx +300 -0
  118. package/templates/auth/components/reset-password-form-wrapper.tsx +190 -0
  119. package/templates/auth/components/signup-form-wrapper.tsx +257 -0
  120. package/templates/auth/components/verify-email-view-wrapper.tsx +265 -0
  121. package/templates/auth/pages/DashboardPage.tsx +12 -0
  122. package/templates/auth/pages/ForgotPasswordPage.tsx +11 -0
  123. package/templates/auth/pages/LoginPage.tsx +11 -0
  124. package/templates/auth/pages/ResetPasswordPage.tsx +11 -0
  125. package/templates/auth/pages/SignUpPage.tsx +11 -0
  126. package/templates/auth/pages/VerifyEmailPage.tsx +11 -0
  127. package/templates/auth/ui/button.tsx +57 -0
  128. package/templates/auth/ui/card.tsx +76 -0
  129. package/templates/auth/ui/field.tsx +242 -0
  130. package/templates/auth/ui/input.tsx +22 -0
  131. package/templates/auth/ui/label.tsx +24 -0
  132. package/templates/auth/ui/separator.tsx +31 -0
  133. package/templates/forgewp-config.ts +70 -3
  134. package/templates/menus.json +12 -10
  135. package/templates/mock-data.json +122 -433
  136. package/templates/products.json +5 -0
  137. package/templates/roles.json +7 -0
  138. package/templates/sessions.json +1 -0
  139. package/templates/users.json +38 -0
  140. package/templates/wordpress.tsx +1835 -1297
  141. package/bin/make-template.js +0 -145
  142. package/lib/block-compiler.js +0 -552
  143. package/lib/functions-builder.js +0 -2044
  144. package/lib/hydration-scanner.js +0 -821
@@ -0,0 +1,400 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ export function buildHydrationEnqueuerPhp(config, assets, mainJs, uniqueMetaKeys, uniqueRichTextKeys, repeaterFieldsPhp, manifestPairs, optionsPairs, themeModsPairs, page_links_php, fontsEnqueue, i18nKeysPhp, headlessScript, currentUserHydrationField, defaultLoginField, providersPhp = '') {
5
+ const css = assets.cssFile.replace(/^assets\//, '');
6
+
7
+ // Only forms need restUrl/restNonce/forms in the hydration payload — kept
8
+ // out entirely for themes without a `forms` config so their generated
9
+ // output stays byte-identical.
10
+ const hasForms = config.forms && Object.keys(config.forms).length > 0;
11
+ const formsHydrationFields = hasForms
12
+ ? `
13
+ 'restUrl' => esc_url_raw( rest_url( 'forgewp/v1' ) ),
14
+ 'restNonce' => is_user_logged_in() ? wp_create_nonce( 'wp_rest' ) : '',
15
+ 'forms' => forgewp_forms_hydration_payload($current_lang),`
16
+ : '';
17
+ const phpStringLiteral = (value) =>
18
+ `'${String(value).replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
19
+ const usesLocalFormsProvider =
20
+ hasForms && config.i18n?.provider === 'local';
21
+ const localLanguageFallbackPhp =
22
+ usesLocalFormsProvider
23
+ ? `
24
+ $forgewp_locales = array(${(config.i18n.locales || []).map(phpStringLiteral).join(', ')});
25
+ $forgewp_request_path = isset($_SERVER['REQUEST_URI']) ? parse_url(wp_unslash($_SERVER['REQUEST_URI']), PHP_URL_PATH) : '';
26
+ $forgewp_home_path = rtrim((string) parse_url(home_url('/'), PHP_URL_PATH), '/');
27
+ if ($forgewp_home_path !== '' && strpos($forgewp_request_path, $forgewp_home_path . '/') === 0) {
28
+ $forgewp_request_path = substr($forgewp_request_path, strlen($forgewp_home_path));
29
+ }
30
+ $forgewp_path_segments = explode('/', trim((string) $forgewp_request_path, '/'));
31
+ $forgewp_path_locale = sanitize_key(reset($forgewp_path_segments));
32
+ if (in_array($forgewp_path_locale, $forgewp_locales, true)) {
33
+ $current_lang = $forgewp_path_locale;
34
+ } else {
35
+ $current_lang = ${phpStringLiteral(config.i18n.defaultLocale || config.i18n.locales?.[0] || 'en')};
36
+ }`
37
+ : `
38
+ $current_lang = get_locale();
39
+ if (strpos($current_lang, '_') !== false) {
40
+ $parts = explode('_', $current_lang);
41
+ $current_lang = $parts[0];
42
+ }`;
43
+ const currentLanguageSetupPhp = hasForms
44
+ ? ` if (function_exists('pll_current_language')) {
45
+ $current_lang = pll_current_language();
46
+ }
47
+ if (empty($current_lang) && defined('ICL_LANGUAGE_CODE')) {
48
+ $current_lang = ICL_LANGUAGE_CODE;
49
+ }`
50
+ : ` if (function_exists('pll_current_language')) {
51
+ $current_lang = pll_current_language();
52
+ } elseif (defined('ICL_LANGUAGE_CODE')) {
53
+ $current_lang = ICL_LANGUAGE_CODE;
54
+ }`;
55
+
56
+ let hydrationEnqueue = '';
57
+ if (mainJs) {
58
+ hydrationEnqueue = `
59
+ // Enqueue React runtime entrypoint and dynamic Selective Hydration assets
60
+ $js_path = get_template_directory() . '/assets/${mainJs}';
61
+ if (file_exists($js_path)) {
62
+ wp_enqueue_script(
63
+ '${config.textDomain}-react-runtime',
64
+ $theme_uri . '/assets/${mainJs}',
65
+ array(),
66
+ filemtime($js_path),
67
+ true
68
+ );
69
+ ${headlessScript}
70
+
71
+ $menu_locations = get_nav_menu_locations();
72
+ $hydrated_menus = array();
73
+ $registered_nav_menus = get_registered_nav_menus();
74
+ $registered_locations = is_array($registered_nav_menus) ? array_keys($registered_nav_menus) : array();
75
+ $current_lang = '';
76
+ ${currentLanguageSetupPhp}
77
+ if (empty($current_lang)) {
78
+ if (function_exists('pll_default_language') && pll_default_language()) {
79
+ $current_lang = pll_default_language();
80
+ } else {
81
+ ${localLanguageFallbackPhp}
82
+ }
83
+ }
84
+ if (!function_exists('forgewp_build_menu_tree')) {
85
+ function forgewp_build_menu_tree($menu_items, $parent_id = 0) {
86
+ $branch = array();
87
+ foreach ($menu_items as $item) {
88
+ if ((int)$item->menu_item_parent === (int)$parent_id) {
89
+ $children = forgewp_build_menu_tree($menu_items, $item->ID);
90
+ $node = array(
91
+ // String, not int. Client state uses this id as an
92
+ // object key, and those keys are always strings.
93
+ // A numeric id never matches, so submenu toggles no-op.
94
+ 'id' => (string) $item->ID,
95
+ 'title' => $item->title,
96
+ 'url' => $item->url,
97
+ );
98
+ if (!empty($item->target)) {
99
+ $node['target'] = $item->target;
100
+ }
101
+ if (!empty($item->classes) && is_array($item->classes)) {
102
+ $classes = array_values(array_filter($item->classes));
103
+ if (!empty($classes)) {
104
+ $node['classes'] = $classes;
105
+ }
106
+ }
107
+ if (!empty($item->description)) {
108
+ $node['description'] = $item->description;
109
+ }
110
+ if (!empty($item->attr_title)) {
111
+ $node['attrTitle'] = $item->attr_title;
112
+ }
113
+ $badge = get_post_meta($item->ID, '_forgewp_menu_badge', true);
114
+ if (!empty($badge)) {
115
+ $node['badge'] = $badge;
116
+ }
117
+ $image = get_post_meta($item->ID, '_forgewp_menu_image', true);
118
+ if (!empty($image)) {
119
+ $node['image'] = $image;
120
+ }
121
+ $icon = get_post_meta($item->ID, '_forgewp_menu_icon', true);
122
+ if (!empty($icon)) {
123
+ $node['icon'] = $icon;
124
+ }
125
+ if (!empty($children)) {
126
+ $node['children'] = $children;
127
+ }
128
+ $branch[] = $node;
129
+ }
130
+ }
131
+ return $branch;
132
+ }
133
+ }
134
+
135
+ foreach ($registered_locations as $loc) {
136
+ $base_loc = $loc;
137
+ if (strpos($loc, '___') !== false) {
138
+ $parts = explode('___', $loc);
139
+ $base_loc = $parts[0];
140
+ }
141
+
142
+ $menu_id = forgewp_get_menu_id_for_lang($base_loc, $current_lang);
143
+ $raw_items = $menu_id ? wp_get_nav_menu_items($menu_id) : array();
144
+ $menu_items = is_array($raw_items) ? $raw_items : array();
145
+ $items_list = forgewp_build_menu_tree($menu_items, 0);
146
+ $hydrated_menus[$base_loc] = $items_list;
147
+ if (!empty($current_lang)) {
148
+ $hydrated_menus[$base_loc . '___' . $current_lang] = $items_list;
149
+ }
150
+ }
151
+
152
+ $page_links = array();
153
+ ${page_links_php}
154
+
155
+ // Inject hydration manifest mappings dynamically
156
+ wp_add_inline_script(
157
+ '${config.textDomain}-react-runtime',
158
+ 'window.forgeWpHydration = Object.assign(window.forgeWpHydration || {}, ' . wp_json_encode(array(
159
+ 'themeUri' => $theme_uri,
160
+ 'menus' => $hydrated_menus,
161
+ 'pageLinks' => $page_links,
162
+ 'loginField' => get_option( 'forgewp_auth_login_field', '${defaultLoginField}' ),${currentUserHydrationField}${formsHydrationFields}
163
+ 'manifest' => array(
164
+ ${manifestPairs}
165
+ ),${providersPhp}
166
+ 'siteSettings' => array(
167
+ 'options' => array(
168
+ ${optionsPairs}
169
+ ),
170
+ 'theme_mods' => array(
171
+ ${themeModsPairs}
172
+ ),
173
+ ),
174
+ ), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE) . ');',
175
+ 'before'
176
+ );
177
+
178
+ // Enqueue Micro-Hydrator orchestrator script
179
+ $hydrator_path = get_template_directory() . '/assets/forgewp-hydrator.js';
180
+ wp_enqueue_script(
181
+ '${config.textDomain}-hydrator',
182
+ $theme_uri . '/assets/forgewp-hydrator.js',
183
+ array('${config.textDomain}-react-runtime'),
184
+ file_exists($hydrator_path) ? filemtime($hydrator_path) : FORGEWP_THEME_VERSION,
185
+ true
186
+ );
187
+
188
+ // Integrate dynamic translation dictionary and Polylang / WPML multi-language data
189
+ $i18n_keys = array(
190
+ ${i18nKeysPhp}
191
+ );
192
+ if (function_exists('pll_register_string')) {
193
+ foreach ($i18n_keys as $key) {
194
+ pll_register_string($key, $key, '${config.textDomain}');
195
+ }
196
+ }
197
+ ${hasForms ? ` $current_lang = forgewp_forms_resolve_lang($current_lang);\n` : ` $current_lang = '';
198
+ if (function_exists('pll_current_language')) {
199
+ $current_lang = pll_current_language();
200
+ }
201
+ if (empty($current_lang)) {
202
+ $current_lang = get_locale();
203
+ }
204
+ `}
205
+
206
+ $json_dict = array();
207
+ $translations_file = get_template_directory() . '/translations.json';
208
+ if (file_exists($translations_file)) {
209
+ $json_data = json_decode(file_get_contents($translations_file), true);
210
+ if (is_array($json_data) && isset($json_data[$current_lang])) {
211
+ $json_dict = $json_data[$current_lang];
212
+ }
213
+ }
214
+
215
+ $translated_dict = array();
216
+ if (is_array($json_dict)) {
217
+ $translated_dict = $json_dict;
218
+ }
219
+ foreach ($i18n_keys as $key) {
220
+ if (!isset($translated_dict[$key])) {
221
+ $translated_dict[$key] = function_exists('pll__') ? pll__($key) : __($key, '${config.textDomain}');
222
+ }
223
+ }
224
+
225
+ $translations = array();
226
+ $home_urls = array();
227
+ if (function_exists('pll_the_languages')) {
228
+ $langs = pll_the_languages(array('raw' => 1));
229
+ if (is_array($langs)) {
230
+ foreach ($langs as $l) {
231
+ $translations[$l['slug']] = $l['url'];
232
+ if (function_exists('pll_home_url')) {
233
+ $home_urls[$l['slug']] = pll_home_url($l['slug']);
234
+ }
235
+ }
236
+ }
237
+ }
238
+ if (empty($home_urls)) {
239
+ $home_urls[$current_lang] = home_url('/');
240
+ }
241
+
242
+ wp_add_inline_script(
243
+ '${config.textDomain}-react-runtime',
244
+ 'window.forgeWpTranslations = ' . wp_json_encode(array(
245
+ 'currentLanguage' => $current_lang,
246
+ 'urls' => $translations,
247
+ 'homeUrls' => $home_urls,
248
+ 'translations' => $translated_dict
249
+ ), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE) . ';',
250
+ 'before'
251
+ );
252
+
253
+ // Static key lists — compile-time-known, independent of whether the
254
+ // current request actually resolves to a post (a 404 or archive page
255
+ // has no $post_id, but $front_page_id below is looked up regardless,
256
+ // so these must never be scoped inside the "$post_id > 0" branch —
257
+ // that previously left them undefined whenever the visited URL had
258
+ // no singular post, throwing "Undefined variable" + "foreach() on
259
+ // null" the moment page_on_front's own hydration ran).
260
+ $registered_keys = array(${Array.from(uniqueMetaKeys).map(k => `'${k}'`).join(', ')});
261
+ $rich_text_keys = array(${uniqueRichTextKeys.map(k => `'${k}'`).join(', ')});
262
+ $repeater_fields_map = array(
263
+ ${repeaterFieldsPhp}
264
+ );
265
+
266
+ $post_custom_fields = array();
267
+ $post_id = get_the_ID();
268
+ if ($post_id > 0) {
269
+ foreach ($registered_keys as $key) {
270
+ if (array_key_exists($key, $repeater_fields_map)) {
271
+ $val = forgewp_get_repeater_field($key, $repeater_fields_map[$key], $post_id);
272
+ // Omit empty so useWpMeta falls back to schema defaults
273
+ if (is_array($val) && count($val) === 0) {
274
+ continue;
275
+ }
276
+ $post_custom_fields[$key] = $val;
277
+ } else {
278
+ $val = null;
279
+ if (function_exists('get_field')) {
280
+ $val = get_field($key, $post_id);
281
+ }
282
+ if ($val === null || $val === false) {
283
+ $val = get_post_meta($post_id, $key, true);
284
+ if (is_string($val) && (strpos($val, '[') === 0 || strpos($val, '{') === 0)) {
285
+ $decoded = json_decode($val, true);
286
+ if (json_last_error() === JSON_ERROR_NONE) {
287
+ $val = $decoded;
288
+ }
289
+ }
290
+ }
291
+ // Match forgewp_get_meta_value: empty → omit (JS uses schema defaults)
292
+ if ($val === null || $val === false || $val === '' || (is_array($val) && count($val) === 0)) {
293
+ continue;
294
+ }
295
+ if (in_array($key, $rich_text_keys) && is_string($val)) {
296
+ $val = wpautop($val);
297
+ }
298
+ $post_custom_fields[$key] = $val;
299
+ }
300
+ }
301
+ }
302
+
303
+ $extra_posts_hydration = array();
304
+ // forgewp_resolve_front_page_id() — not the raw page_on_front option
305
+ // — so this hydrates the CURRENT-language homepage's own fields (a
306
+ // Polylang site has a separate post per language), not whichever
307
+ // post happens to be the raw, non-language-aware option value.
308
+ $front_page_id = forgewp_resolve_front_page_id();
309
+ if ($front_page_id > 0 && $front_page_id != $post_id) {
310
+ $front_custom_fields = array();
311
+ foreach ($registered_keys as $key) {
312
+ if (array_key_exists($key, $repeater_fields_map)) {
313
+ $val = forgewp_get_repeater_field($key, $repeater_fields_map[$key], $front_page_id);
314
+ if (is_array($val) && count($val) === 0) {
315
+ continue;
316
+ }
317
+ $front_custom_fields[$key] = $val;
318
+ } else {
319
+ $val = null;
320
+ if (function_exists('get_field')) {
321
+ $val = get_field($key, $front_page_id);
322
+ }
323
+ if ($val === null || $val === false) {
324
+ $val = get_post_meta($front_page_id, $key, true);
325
+ if (is_string($val) && (strpos($val, '[') === 0 || strpos($val, '{') === 0)) {
326
+ $decoded = json_decode($val, true);
327
+ if (json_last_error() === JSON_ERROR_NONE) {
328
+ $val = $decoded;
329
+ }
330
+ }
331
+ }
332
+ if ($val === null || $val === false || $val === '' || (is_array($val) && count($val) === 0)) {
333
+ continue;
334
+ }
335
+ if (in_array($key, $rich_text_keys) && is_string($val)) {
336
+ $val = wpautop($val);
337
+ }
338
+ $front_custom_fields[$key] = $val;
339
+ }
340
+ }
341
+ $extra_posts_hydration[$front_page_id] = array(
342
+ 'id' => $front_page_id,
343
+ 'customFields' => $front_custom_fields
344
+ );
345
+ }
346
+
347
+ wp_add_inline_script(
348
+ '${config.textDomain}-react-runtime',
349
+ 'if (!window.forgeWpHydration) { window.forgeWpHydration = {}; } ' .
350
+ 'window.forgeWpHydration.post = ' . wp_json_encode(array(
351
+ 'id' => $post_id,
352
+ // Cast so empty maps encode as {} not [] (JSON)
353
+ 'customFields' => (object)$post_custom_fields
354
+ ), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE) . ';' .
355
+ 'window.forgeWpHydration.posts = ' . wp_json_encode((object)$extra_posts_hydration, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE) . ';',
356
+ 'before'
357
+ );
358
+ }
359
+
360
+ if (!function_exists('${config.textDomain.replace(/-/g, '_')}_script_loader_tag')) {
361
+ function ${config.textDomain.replace(/-/g, '_')}_script_loader_tag($tag, $handle, $src) {
362
+ if (in_array($handle, array('${config.textDomain}-react-runtime', '${config.textDomain}-hydrator'), true)) {
363
+ $tag = preg_replace_callback('/(<script\\b[^>]*src=[^>]*>)/i', function($matches) {
364
+ $script_open = $matches[1];
365
+ $script_open = preg_replace('/\\stype\\s*=\\s*([\\\'"])[^\\\'"]*\\1/i', '', $script_open);
366
+ $script_open = preg_replace('/<script\\b/i', '<script type="module"', $script_open);
367
+ return $script_open;
368
+ }, $tag, 1);
369
+ }
370
+ return $tag;
371
+ }
372
+ add_filter('script_loader_tag', '${config.textDomain.replace(/-/g, '_')}_script_loader_tag', 10, 3);
373
+ }
374
+ `;
375
+ }
376
+
377
+ const mainAssetEnqueuer = `
378
+ /**
379
+ * Enqueue compiled theme assets.
380
+ */
381
+ function forgewp_enqueue_assets(): void {
382
+ $theme_uri = get_template_directory_uri();
383
+ $css_path = get_template_directory() . '/assets/${css}';
384
+ ${fontsEnqueue}
385
+
386
+ if (file_exists($css_path)) {
387
+ wp_enqueue_style(
388
+ '${config.textDomain}-app',
389
+ $theme_uri . '/assets/${css}',
390
+ array(),
391
+ filemtime($css_path)
392
+ );
393
+ }
394
+ ${hydrationEnqueue}
395
+ }
396
+ add_action('enqueue_block_assets', 'forgewp_enqueue_assets');
397
+ `;
398
+
399
+ return mainAssetEnqueuer;
400
+ }