@forgewp/compiler 0.4.3 → 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 (133) hide show
  1. package/README.md +59 -0
  2. package/bin/add.js +20 -14
  3. package/bin/cli.js +145 -70
  4. package/bin/doctor.js +21 -12
  5. package/bin/export.js +69 -20
  6. package/bin/fresh.js +5 -0
  7. package/bin/i18n-extract.js +3 -10
  8. package/bin/i18n-translate.js +15 -24
  9. package/bin/make-block.js +13 -0
  10. package/bin/make-editable.js +166 -0
  11. package/bin/make-form.js +131 -0
  12. package/bin/make-loop.js +40 -43
  13. package/bin/make-mail.js +27 -0
  14. package/bin/make-post-type.js +57 -29
  15. package/bin/make-sandbox.js +62 -11
  16. package/bin/make-section.js +221 -0
  17. package/bin/make-shell.js +375 -0
  18. package/bin/repair.js +14 -26
  19. package/bin/seed.js +119 -0
  20. package/bin/sync-routes.js +107 -85
  21. package/lib/adapters/html-adapter.js +5 -0
  22. package/lib/adapters/stub-adapter.js +4 -0
  23. package/lib/add-component.js +153 -92
  24. package/lib/asset-loader.js +313 -3
  25. package/lib/assets/asset-graph.js +432 -0
  26. package/lib/assets/asset-pipeline.js +125 -0
  27. package/lib/assets/diagnostics-report.js +345 -0
  28. package/lib/assets/external-fetcher.js +173 -0
  29. package/lib/assets/font-orchestrator.js +348 -0
  30. package/lib/assets/font-usage-scanner.js +242 -0
  31. package/lib/assets/image-optimizer.js +229 -0
  32. package/lib/assets/index.js +51 -0
  33. package/lib/assets/preload-generator.js +70 -0
  34. package/lib/assets/public-url.js +153 -0
  35. package/lib/block-authoring-help.js +11 -0
  36. package/lib/blocks/ast-parser.js +67 -0
  37. package/lib/blocks/discover-blocks.js +146 -0
  38. package/lib/blocks/editor-scope.js +733 -0
  39. package/lib/blocks/editor-transpiler.js +1229 -0
  40. package/lib/blocks/index.js +2838 -0
  41. package/lib/blocks/php-transpiler.js +4928 -0
  42. package/lib/blocks/scanner.js +1487 -0
  43. package/lib/blocks/shared-utils.js +290 -0
  44. package/lib/blocks/source-sanitize.js +1449 -0
  45. package/lib/blueprints.js +31 -91
  46. package/lib/build-assets.js +38 -33
  47. package/lib/collect-page-template-routes.js +102 -0
  48. package/lib/collect-static-routes.js +175 -0
  49. package/lib/diagnostics.js +58 -33
  50. package/lib/export-theme.js +84 -5
  51. package/lib/extract-template-content.js +90 -0
  52. package/lib/forms-preview.js +42 -0
  53. package/lib/functions/cpt-routing.js +69 -0
  54. package/lib/functions/forms.js +2461 -0
  55. package/lib/functions/hydration-enqueuer.js +400 -0
  56. package/lib/functions/index.js +1623 -0
  57. package/lib/functions/load-menus.js +112 -0
  58. package/lib/functions/load-translations.js +148 -0
  59. package/lib/functions/rest-endpoints.js +2923 -0
  60. package/lib/functions/routing.js +765 -0
  61. package/lib/functions/seed-mock-data.js +392 -0
  62. package/lib/functions/seed-products.js +481 -0
  63. package/lib/functions/settings-page.js +273 -0
  64. package/lib/functions/theme-setup.js +494 -0
  65. package/lib/generate-theme.js +1916 -339
  66. package/lib/hydration/editable-schemas.js +332 -0
  67. package/lib/hydration/enter-hidden-initial.js +65 -0
  68. package/lib/hydration/form-schemas.js +227 -0
  69. package/lib/hydration/index.js +12 -0
  70. package/lib/hydration/is-interactive.js +951 -0
  71. package/lib/hydration/island-split.js +1231 -0
  72. package/lib/hydration/islands-scanner.js +599 -0
  73. package/lib/hydration/resolve-island-chunk.js +103 -0
  74. package/lib/hydration/resolve-island-component.js +52 -0
  75. package/lib/hydration/rollup-inputs.js +81 -0
  76. package/lib/hydration/root-class-extractor.js +220 -0
  77. package/lib/hydration/scan-app-providers.js +154 -0
  78. package/lib/hydration/static-lint.js +382 -0
  79. package/lib/hydration/transform.js +257 -0
  80. package/lib/hydration/uses-block-area.js +49 -0
  81. package/lib/hydration/utils.js +119 -0
  82. package/lib/i18n/translation-cache.js +141 -0
  83. package/lib/icon-registry.js +72 -0
  84. package/lib/index.d.ts +287 -8
  85. package/lib/index.js +2 -1
  86. package/lib/load-config.js +338 -242
  87. package/lib/mail-templates.js +37 -0
  88. package/lib/markup-processor.js +388 -102
  89. package/lib/node-runtime.js +7 -0
  90. package/lib/page-config-plugin.js +64 -20
  91. package/lib/php-builders.js +383 -44
  92. package/lib/react-adapter.js +983 -235
  93. package/lib/registration-metadata.js +11 -0
  94. package/lib/render-theme.mts +1410 -616
  95. package/lib/route-layout.js +76 -0
  96. package/lib/template-discovery.js +58 -0
  97. package/lib/theme-compatibility.js +32 -0
  98. package/lib/validate-export.js +2 -2
  99. package/lib/validate.js +27 -5
  100. package/lib/virtual-plugin.js +319 -0
  101. package/lib/walkthrough.md +26 -0
  102. package/package.json +16 -5
  103. package/templates/auth/cms/email-logs.json +9 -0
  104. package/templates/auth/cms/menus.json +11 -0
  105. package/templates/auth/cms/mock-data.json +123 -0
  106. package/templates/auth/cms/roles.json +7 -0
  107. package/templates/auth/cms/sessions.json +1 -0
  108. package/templates/auth/cms/site-settings.json +28 -0
  109. package/templates/auth/cms/translations.json +4 -0
  110. package/templates/auth/cms/users.json +46 -0
  111. package/templates/auth/components/forgot-password-form-wrapper.tsx +10 -2
  112. package/templates/auth/components/login-form-wrapper.tsx +118 -21
  113. package/templates/auth/components/reset-password-form-wrapper.tsx +10 -2
  114. package/templates/auth/components/signup-form-wrapper.tsx +2 -2
  115. package/templates/auth/components/verify-email-view-wrapper.tsx +5 -5
  116. package/templates/auth/pages/DashboardPage.tsx +1 -4
  117. package/templates/auth/pages/ForgotPasswordPage.tsx +1 -4
  118. package/templates/auth/pages/LoginPage.tsx +1 -4
  119. package/templates/auth/pages/ResetPasswordPage.tsx +1 -4
  120. package/templates/auth/pages/SignUpPage.tsx +1 -4
  121. package/templates/auth/pages/VerifyEmailPage.tsx +1 -4
  122. package/templates/auth/ui/button.tsx +57 -0
  123. package/templates/auth/ui/card.tsx +76 -0
  124. package/templates/auth/ui/field.tsx +242 -0
  125. package/templates/auth/ui/input.tsx +22 -0
  126. package/templates/auth/ui/label.tsx +24 -0
  127. package/templates/auth/ui/separator.tsx +31 -0
  128. package/templates/menus.json +12 -10
  129. package/templates/users.json +8 -4
  130. package/templates/wordpress.tsx +690 -245
  131. package/lib/block-compiler.js +0 -552
  132. package/lib/functions-builder.js +0 -2970
  133. package/lib/hydration-scanner.js +0 -833
@@ -0,0 +1,765 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { buildPhpIconSvgMapLiteral } from '../icon-registry.js';
4
+
5
+ export function buildRoutingPhp(
6
+ config,
7
+ pagesPhpArray,
8
+ schemaDefaultsPhp,
9
+ menuItemsPhpArray,
10
+ socialSettingsPhp,
11
+ postTypes,
12
+ ) {
13
+ const iconSvgMapPhp = buildPhpIconSvgMapLiteral();
14
+ const autoCreationPhp = `
15
+ if (!function_exists('forgewp_ensure_route_parent_page')) {
16
+ function forgewp_ensure_route_parent_page(string $path): int {
17
+ $path = trim($path, '/');
18
+ if ($path === '') {
19
+ return 0;
20
+ }
21
+
22
+ $existing_page = get_page_by_path($path, OBJECT, 'page');
23
+ if ($existing_page) {
24
+ return (int) $existing_page->ID;
25
+ }
26
+
27
+ $parts = explode('/', $path);
28
+ $post_name = sanitize_title(array_pop($parts));
29
+ $parent_id = !empty($parts)
30
+ ? forgewp_ensure_route_parent_page(implode('/', $parts))
31
+ : 0;
32
+ if (!empty($parts) && $parent_id <= 0) {
33
+ return 0;
34
+ }
35
+
36
+ $post_title = ucwords(str_replace(array('-', '_'), ' ', $post_name));
37
+ $page_id = wp_insert_post(array(
38
+ 'post_title' => $post_title,
39
+ 'post_name' => $post_name,
40
+ 'post_parent' => $parent_id,
41
+ 'post_status' => 'publish',
42
+ 'post_type' => 'page',
43
+ ), true);
44
+ if (is_wp_error($page_id) || $page_id <= 0) {
45
+ return 0;
46
+ }
47
+
48
+ update_post_meta($page_id, '_wp_page_template', 'page-placeholder.php');
49
+ update_post_meta($page_id, 'forgewp_route_parent', '1');
50
+
51
+ return (int) $page_id;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Automatically create routes defined in menus.json and assign custom page templates upon theme activation.
57
+ */
58
+ function forgewp_auto_create_pages_and_menus(): void {
59
+ $activated_option = '${config.textDomain}_activated_' . str_replace('.', '_', FORGEWP_THEME_VERSION);
60
+ $has_run = get_option($activated_option) === 'yes';
61
+
62
+ $schema_defaults = array(
63
+ ${schemaDefaultsPhp}
64
+ );
65
+
66
+ $pages = array(
67
+ ${pagesPhpArray}
68
+ );
69
+
70
+ foreach ($pages as $p) {
71
+ $slug_path = trim($p['slug'], '/');
72
+ $parent_id = 0;
73
+ $post_name = $slug_path;
74
+ if (strpos($slug_path, '/') !== false) {
75
+ $parts = explode('/', $slug_path);
76
+ $post_name = array_pop($parts);
77
+ $parent_path = implode('/', $parts);
78
+ $parent_id = forgewp_ensure_route_parent_page($parent_path);
79
+ if ($parent_id <= 0) {
80
+ error_log('[ForgeWP] Could not provision parent path for route: ' . $slug_path);
81
+ continue;
82
+ }
83
+ }
84
+
85
+ $existing_page = get_page_by_path($slug_path, OBJECT, 'page');
86
+ $page_id = 0;
87
+ if (!$existing_page) {
88
+ $page_args = array(
89
+ 'post_title' => $p['title'],
90
+ 'post_name' => $post_name,
91
+ 'post_parent' => $parent_id,
92
+ 'post_status' => 'publish',
93
+ 'post_type' => 'page',
94
+ );
95
+ if (!empty($p['description'])) {
96
+ $page_args['post_content'] = $p['description'];
97
+ }
98
+ $page_id = wp_insert_post($page_args);
99
+ if (!is_wp_error($page_id) && $page_id > 0) {
100
+ update_post_meta($page_id, '_wp_page_template', $p['template']);
101
+ if (!empty($p['description'])) {
102
+ update_post_meta($page_id, '_forgewp_page_description', $p['description']);
103
+ }
104
+ }
105
+ } else {
106
+ $page_id = $existing_page->ID;
107
+ $current_tpl = get_post_meta($page_id, '_wp_page_template', true);
108
+ if ($current_tpl === 'page-placeholder.php' || empty($current_tpl) || (!$has_run && $current_tpl !== $p['template'])) {
109
+ update_post_meta($page_id, '_wp_page_template', $p['template']);
110
+ }
111
+ if (!empty($p['title']) && ($existing_page->post_title !== $p['title'] && ($current_tpl === 'page-placeholder.php' || !$has_run))) {
112
+ wp_update_post(array(
113
+ 'ID' => $page_id,
114
+ 'post_title' => $p['title'],
115
+ ));
116
+ }
117
+ if (!empty($p['description'])) {
118
+ update_post_meta($page_id, '_forgewp_page_description', $p['description']);
119
+ }
120
+ }
121
+
122
+ // Tag the page with a durable route marker, independent of whichever
123
+ // template it's currently assigned (the template is user-editable in
124
+ // wp-admin — e.g. switching to a generic block-based template — and
125
+ // link resolution elsewhere (forgewp_resolve_route_page_id) must keep
126
+ // working even after that happens). Runs unconditionally (not gated
127
+ // by $has_run) so it self-heals on every request, including on sites
128
+ // that were already activated before this marker was introduced.
129
+ if ($page_id > 0) {
130
+ $route_id = preg_replace('/^page-|\.php$/', '', $p['template']);
131
+ if (get_post_meta($page_id, 'forgewp_route_id', true) !== $route_id) {
132
+ update_post_meta($page_id, 'forgewp_route_id', $route_id);
133
+ }
134
+ }
135
+
136
+ // Phase 3: Automatically seed default post meta fields if not already populated for ALL pages using this template
137
+ if (isset($schema_defaults[$p['template']])) {
138
+ $matching_pages = get_posts(array(
139
+ 'post_type' => 'page',
140
+ 'post_status'=> 'any',
141
+ 'lang' => '',
142
+ 'meta_query' => array(
143
+ array(
144
+ 'key' => '_wp_page_template',
145
+ 'value' => $p['template'],
146
+ 'compare' => '='
147
+ )
148
+ ),
149
+ 'posts_per_page' => -1
150
+ ));
151
+
152
+ $pages_to_seed = array();
153
+ if ($page_id > 0) {
154
+ $pages_to_seed[] = $page_id;
155
+ }
156
+ if (is_array($matching_pages)) {
157
+ foreach ($matching_pages as $mp) {
158
+ if (!in_array($mp->ID, $pages_to_seed)) {
159
+ $pages_to_seed[] = $mp->ID;
160
+ }
161
+ }
162
+ }
163
+
164
+ foreach ($pages_to_seed as $pid) {
165
+ foreach ($schema_defaults[$p['template']] as $meta_key => $default_val) {
166
+ // If it is a JSON repeater, decode and seed individual ACF sub-fields
167
+ if (is_string($default_val) && (strpos($default_val, '[') === 0 || strpos($default_val, '{') === 0)) {
168
+ $decoded = json_decode($default_val, true);
169
+ if (is_array($decoded)) {
170
+ // Check if the repeater is already populated by checking its count meta
171
+ $current_count = get_post_meta($pid, $meta_key, true);
172
+ if (empty($current_count) || !is_numeric($current_count) || intval($current_count) === 0) {
173
+ // Seed the repeater count and values
174
+ update_post_meta($pid, $meta_key, count($decoded));
175
+ foreach ($decoded as $index => $row) {
176
+ if (is_array($row)) {
177
+ foreach ($row as $sub_key => $sub_val) {
178
+ update_post_meta($pid, "{$meta_key}_{$index}_{$sub_key}", $sub_val);
179
+ }
180
+ }
181
+ }
182
+
183
+ // Also store the raw JSON string as fallback for React direct hooks
184
+ update_post_meta($pid, "{$meta_key}_json_fallback", $default_val);
185
+ }
186
+
187
+ // ALWAYS enforce ACF key reference mappings for fields to display in WP Admin
188
+ update_post_meta($pid, "_{$meta_key}", "field_{$meta_key}");
189
+ $count_to_map = get_post_meta($pid, $meta_key, true);
190
+ if (is_numeric($count_to_map)) {
191
+ for ($index = 0; $index < intval($count_to_map); $index++) {
192
+ foreach (array_keys($decoded[0] ?? array()) as $sub_key) {
193
+ update_post_meta($pid, "_{$meta_key}_{$index}_{$sub_key}", "field_{$meta_key}_{$sub_key}");
194
+ }
195
+ }
196
+ }
197
+ continue;
198
+ }
199
+ }
200
+
201
+ $current_val = get_post_meta($pid, $meta_key, true);
202
+ if (empty($current_val)) {
203
+ update_post_meta($pid, $meta_key, $default_val);
204
+ }
205
+ }
206
+ }
207
+ }
208
+ }
209
+
210
+ $menu_structure = array(
211
+ ${menuItemsPhpArray}
212
+ );
213
+
214
+ if (!function_exists('forgewp_seed_menu_items')) {
215
+ function forgewp_seed_menu_items($menu_id, $items, $parent_id = 0): void {
216
+ if (!is_array($items)) return;
217
+ foreach ($items as $item) {
218
+ $item_title = $item['title'] ?? '';
219
+ $item_url = $item['url'] ?? '';
220
+
221
+ $object_id = 0;
222
+ $object_type = 'custom';
223
+ $target_url = $item_url;
224
+
225
+ if (strpos($item_url, '/') === 0) {
226
+ $slug = trim($item_url, '/');
227
+ if ($slug === '') {
228
+ $target_url = home_url('/');
229
+ } else {
230
+ $page = get_page_by_path($slug);
231
+ if ($page) {
232
+ $object_id = $page->ID;
233
+ $object_type = 'page';
234
+ $target_url = get_permalink($page->ID);
235
+ } else {
236
+ $target_url = home_url($item_url);
237
+ }
238
+ }
239
+ }
240
+
241
+ $menu_item_args = array(
242
+ 'menu-item-title' => $item_title,
243
+ 'menu-item-url' => $target_url,
244
+ 'menu-item-object-id' => $object_id,
245
+ 'menu-item-object' => $object_type === 'page' ? 'page' : '',
246
+ 'menu-item-type' => $object_type === 'page' ? 'post_type' : 'custom',
247
+ 'menu-item-status' => 'publish',
248
+ 'menu-item-parent-id' => (int)$parent_id,
249
+ );
250
+ if (!empty($item['classes'])) {
251
+ $menu_item_args['menu-item-classes'] = is_array($item['classes']) ? implode(' ', $item['classes']) : $item['classes'];
252
+ }
253
+ if (!empty($item['description'])) {
254
+ $menu_item_args['menu-item-description'] = $item['description'];
255
+ }
256
+ if (!empty($item['target'])) {
257
+ $menu_item_args['menu-item-target'] = $item['target'];
258
+ }
259
+ if (!empty($item['attrTitle'])) {
260
+ $menu_item_args['menu-item-attr-title'] = $item['attrTitle'];
261
+ }
262
+
263
+ $item_id = wp_update_nav_menu_item($menu_id, 0, $menu_item_args);
264
+
265
+ if (!is_wp_error($item_id) && $item_id > 0) {
266
+ if (!empty($item['badge'])) {
267
+ update_post_meta($item_id, '_forgewp_menu_badge', sanitize_text_field($item['badge']));
268
+ }
269
+ if (!empty($item['image'])) {
270
+ update_post_meta($item_id, '_forgewp_menu_image', esc_url_raw($item['image']));
271
+ }
272
+ if (!empty($item['icon'])) {
273
+ update_post_meta($item_id, '_forgewp_menu_icon', sanitize_text_field($item['icon']));
274
+ }
275
+ if (!empty($item['children']) && is_array($item['children'])) {
276
+ forgewp_seed_menu_items($menu_id, $item['children'], $item_id);
277
+ }
278
+ }
279
+ }
280
+ }
281
+ }
282
+
283
+ $locations = get_theme_mod('nav_menu_locations');
284
+ if (!is_array($locations)) {
285
+ $locations = array();
286
+ }
287
+
288
+ foreach ($menu_structure as $location => $items) {
289
+ $menu_name = ucfirst($location) . ' Navigation';
290
+ $menu_exists = false;
291
+
292
+ if (!empty($locations[$location])) {
293
+ $menu_obj = wp_get_nav_menu_object($locations[$location]);
294
+ if ($menu_obj) {
295
+ $menu_exists = true;
296
+ }
297
+ }
298
+
299
+ if (!$menu_exists) {
300
+ $menu_obj = wp_get_nav_menu_object($menu_name);
301
+ if ($menu_obj) {
302
+ $menu_exists = true;
303
+ $locations[$location] = $menu_obj->term_id;
304
+ set_theme_mod('nav_menu_locations', $locations);
305
+ }
306
+ }
307
+
308
+ if (!$menu_exists) {
309
+ $menu_id = wp_create_nav_menu($menu_name);
310
+ if (!is_wp_error($menu_id) && $menu_id > 0) {
311
+ $locations[$location] = $menu_id;
312
+ set_theme_mod('nav_menu_locations', $locations);
313
+ forgewp_seed_menu_items($menu_id, $items, 0);
314
+ }
315
+ }
316
+ }
317
+
318
+ if (!$has_run) {
319
+ update_option($activated_option, 'yes');
320
+ }
321
+ }
322
+ add_action('after_switch_theme', 'forgewp_auto_create_pages_and_menus');
323
+ add_action('init', 'forgewp_auto_create_pages_and_menus', 99);
324
+
325
+ /**
326
+ * Register customizer settings for theme modifications (e.g., footer_text).
327
+ * This allows theme mods to be managed via WordPress Customizer and WP Admin.
328
+ */
329
+ function forgewp_register_theme_customizer_settings($wp_customize): void {
330
+ // Register footer_text theme modification
331
+ $wp_customize->add_setting(
332
+ 'footer_text',
333
+ array(
334
+ 'default' => '',
335
+ 'sanitize_callback' => 'sanitize_text_field',
336
+ )
337
+ );
338
+
339
+ $wp_customize->add_control(
340
+ 'footer_text',
341
+ array(
342
+ 'label' => __('Footer Text', '${config.textDomain}'),
343
+ 'section' => 'title_tagline',
344
+ 'type' => 'textarea',
345
+ 'description' => __('Custom text displayed in the theme footer.', '${config.textDomain}'),
346
+ )
347
+ );
348
+
349
+ // ── Social Media Section ──
350
+ $wp_customize->add_section(
351
+ 'forgewp_social_section',
352
+ array(
353
+ 'title' => __('Social Media Accounts', '${config.textDomain}'),
354
+ 'priority' => 35,
355
+ 'description' => __('Manage your social media profile URLs.', '${config.textDomain}'),
356
+ )
357
+ );
358
+
359
+ ${socialSettingsPhp}
360
+
361
+ // ── Contact Information Section ──
362
+ $wp_customize->add_section(
363
+ 'forgewp_contact_section',
364
+ array(
365
+ 'title' => __('Contact Information', '${config.textDomain}'),
366
+ 'priority' => 36,
367
+ 'description' => __('Manage contact details used throughout the site.', '${config.textDomain}'),
368
+ )
369
+ );
370
+
371
+ // contact_phone
372
+ $wp_customize->add_setting(
373
+ 'contact_phone',
374
+ array(
375
+ 'type' => 'option',
376
+ 'default' => '',
377
+ 'sanitize_callback' => 'sanitize_text_field',
378
+ )
379
+ );
380
+ $wp_customize->add_control(
381
+ 'contact_phone',
382
+ array(
383
+ 'label' => __('Phone Number', '${config.textDomain}'),
384
+ 'section' => 'forgewp_contact_section',
385
+ 'type' => 'text',
386
+ )
387
+ );
388
+
389
+ // contact_email
390
+ $wp_customize->add_setting(
391
+ 'contact_email',
392
+ array(
393
+ 'type' => 'option',
394
+ 'default' => '',
395
+ 'sanitize_callback' => 'sanitize_email',
396
+ )
397
+ );
398
+ $wp_customize->add_control(
399
+ 'contact_email',
400
+ array(
401
+ 'label' => __('Email Address', '${config.textDomain}'),
402
+ 'section' => 'forgewp_contact_section',
403
+ 'type' => 'email',
404
+ )
405
+ );
406
+
407
+ // contact_address
408
+ $wp_customize->add_setting(
409
+ 'contact_address',
410
+ array(
411
+ 'type' => 'option',
412
+ 'default' => '',
413
+ 'sanitize_callback' => 'sanitize_textarea_field',
414
+ )
415
+ );
416
+ $wp_customize->add_control(
417
+ 'contact_address',
418
+ array(
419
+ 'label' => __('Address', '${config.textDomain}'),
420
+ 'section' => 'forgewp_contact_section',
421
+ 'type' => 'textarea',
422
+ )
423
+ );
424
+
425
+ // business_hours
426
+ $wp_customize->add_setting(
427
+ 'business_hours',
428
+ array(
429
+ 'type' => 'option',
430
+ 'default' => '',
431
+ 'sanitize_callback' => 'sanitize_text_field',
432
+ )
433
+ );
434
+ $wp_customize->add_control(
435
+ 'business_hours',
436
+ array(
437
+ 'label' => __('Business Hours', '${config.textDomain}'),
438
+ 'section' => 'forgewp_contact_section',
439
+ 'type' => 'text',
440
+ )
441
+ );
442
+ }
443
+ add_action('customize_register', 'forgewp_register_theme_customizer_settings');
444
+
445
+ /**
446
+ * Set default footer_text on theme activation if not already set.
447
+ */
448
+ function forgewp_set_default_footer_text(): void {
449
+ if (!get_theme_mod('footer_text')) {
450
+ set_theme_mod('footer_text', '© ' . date('Y') . ' ' . get_bloginfo('name') . '. ' . __('All rights reserved.', '${config.textDomain}'));
451
+ }
452
+ }
453
+ add_action('after_switch_theme', 'forgewp_set_default_footer_text');
454
+ `;
455
+
456
+ const templateResolutionsPhp = `
457
+ /**
458
+ * Resolves the active template head HTML file path dynamically.
459
+ */
460
+ function forgewp_resolve_head_target() {
461
+ $dir = get_template_directory() . '/forgewp-static';
462
+
463
+ // 1. Custom Page Template
464
+ $template_slug = get_page_template_slug();
465
+ if ($template_slug) {
466
+ $clean_slug = str_replace(array('page-', '.php'), '', $template_slug);
467
+ $target = "{$dir}/template-{$clean_slug}-head.html";
468
+ if (file_exists($target)) {
469
+ return $target;
470
+ }
471
+ }
472
+
473
+ // 2. Singular Custom Post Type / Post / Page
474
+ if (is_singular()) {
475
+ $post_type = get_post_type();
476
+ $target = "{$dir}/single-{$post_type}-head.html";
477
+ if (file_exists($target)) {
478
+ return $target;
479
+ }
480
+ $target = "{$dir}/single-head.html";
481
+ if (file_exists($target)) {
482
+ return $target;
483
+ }
484
+ }
485
+
486
+ // 3. Taxonomy Archive
487
+ if (is_tax()) {
488
+ $tax = get_query_var('taxonomy');
489
+ $target = "{$dir}/taxonomy-{$tax}-head.html";
490
+ if (file_exists($target)) {
491
+ return $target;
492
+ }
493
+ $target = "{$dir}/taxonomy-head.html";
494
+ if (file_exists($target)) {
495
+ return $target;
496
+ }
497
+ }
498
+
499
+ // 4. Post Type Archive
500
+ if (is_post_type_archive()) {
501
+ $post_type = get_query_var('post_type');
502
+ $target = "{$dir}/archive-{$post_type}-head.html";
503
+ if (file_exists($target)) {
504
+ return $target;
505
+ }
506
+ }
507
+
508
+ // 5. General Archive / Category / Tag
509
+ if (is_archive()) {
510
+ $target = "{$dir}/archive-head.html";
511
+ if (file_exists($target)) {
512
+ return $target;
513
+ }
514
+ }
515
+
516
+ // 6. Default Head
517
+ return "{$dir}/head.html";
518
+ }
519
+
520
+ /**
521
+ * Whether a known SEO plugin (Yoast, RankMath, All in One SEO, or SEOPress)
522
+ * is currently active. Single source of truth — everywhere ForgeWP needs to
523
+ * yield head-content/title/sitemap responsibility to a standard SEO plugin
524
+ * calls this instead of re-deriving its own plugin-detection condition, so
525
+ * the detected plugin list can't drift out of sync between call sites.
526
+ */
527
+ function forgewp_seo_plugin_active() {
528
+ return defined('WPSEO_VERSION')
529
+ || class_exists('RankMath')
530
+ || class_exists('All_in_One_SEO_Pack')
531
+ || defined('AIOSEO_VERSION')
532
+ || class_exists('SEOPress\\Services\\Title');
533
+ }
534
+
535
+ /**
536
+ * Custom document title filter for ForgeWP.
537
+ * Maps title enqueued via <WpHead /> in React to WordPress.
538
+ */
539
+ function forgewp_custom_document_title( $title ) {
540
+ if ( forgewp_seo_plugin_active() ) {
541
+ return $title;
542
+ }
543
+ $target = forgewp_resolve_head_target();
544
+ if ( file_exists( $target ) ) {
545
+ ob_start();
546
+ include $target;
547
+ $content = ob_get_clean();
548
+ if ( preg_match( '/<title>(.*?)<\\/title>/is', $content, $matches ) ) {
549
+ return html_entity_decode( trim( $matches[1] ), ENT_QUOTES, 'UTF-8' );
550
+ }
551
+ }
552
+ return $title;
553
+ }
554
+ add_filter( 'pre_get_document_title', 'forgewp_custom_document_title', 999 );
555
+
556
+ /**
557
+ * Disable WordPress legacy emoji conversion to match native client-side rendering.
558
+ */
559
+ function forgewp_disable_emojis(): void {
560
+ remove_action('wp_head', 'print_emoji_detection_script', 7);
561
+ remove_action('admin_print_scripts', 'print_emoji_detection_script');
562
+ remove_action('wp_print_styles', 'print_emoji_styles');
563
+ remove_action('admin_print_styles', 'print_emoji_styles');
564
+ remove_filter('the_content_feed', 'wp_staticize_emoji');
565
+ remove_filter('comment_text_rss', 'wp_staticize_emoji');
566
+ remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
567
+ add_filter('tiny_mce_plugins', 'forgewp_disable_emojis_tinymce');
568
+ add_filter('wp_resource_hints', 'forgewp_disable_emojis_remove_dns_prefetch', 10, 2);
569
+ }
570
+ add_action('init', 'forgewp_disable_emojis');
571
+
572
+ function forgewp_disable_emojis_tinymce($plugins) {
573
+ return is_array($plugins) ? array_diff($plugins, array('wpemoji')) : array();
574
+ }
575
+
576
+ function forgewp_disable_emojis_remove_dns_prefetch($urls, $relation_type) {
577
+ if ('dns-prefetch' === $relation_type) {
578
+ $emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/2.2.1/svg/');
579
+ $urls = array_diff($urls, array($emoji_svg_url));
580
+ }
581
+ return $urls;
582
+ }
583
+
584
+ /**
585
+ * Isomorphic static translation bridge for ForgeWP.
586
+ * Hooks into WordPress gettext to translate server-side static strings
587
+ * using translations.json and the active Polylang language.
588
+ */
589
+ function forgewp_gettext_translation_bridge($translated, $text, $domain) {
590
+ if ($domain === '${config.textDomain}') {
591
+ static $translations = null;
592
+ static $translations_mtime = null;
593
+ $file = get_template_directory() . '/translations.json';
594
+ // Keyed on the file's mtime rather than just "already loaded once",
595
+ // so a redeployed translations.json takes effect immediately within
596
+ // this worker instead of only after it's recycled — PHP static
597
+ // function-local variables otherwise persist across every request a
598
+ // PHP-FPM worker serves, not just the one that first populated them.
599
+ $current_mtime = file_exists($file) ? filemtime($file) : false;
600
+ if ($translations === null || $translations_mtime !== $current_mtime) {
601
+ if ($current_mtime !== false) {
602
+ $translations = json_decode(file_get_contents($file), true);
603
+ } else {
604
+ $translations = array();
605
+ }
606
+ $translations_mtime = $current_mtime;
607
+ }
608
+ $type = '';
609
+ if (function_exists('pll_current_language')) {
610
+ $type = pll_current_language();
611
+ }
612
+ if (empty($type)) {
613
+ $type = get_locale();
614
+ }
615
+ $type = substr($type, 0, 2);
616
+ if (is_array($translations) && isset($translations[$type][$text])) {
617
+ return $translations[$type][$text];
618
+ }
619
+ }
620
+ return $translated;
621
+ }
622
+ add_filter('gettext', 'forgewp_gettext_translation_bridge', 10, 3);
623
+ add_filter('ngettext', 'forgewp_gettext_translation_bridge', 10, 3);
624
+
625
+ /**
626
+ * Dynamic SVG theme icon rendering callback.
627
+ * Maps dynamic icon slugs to inline SVGs from Lucide registry, custom uploads, or Dashicons.
628
+ */
629
+ function forgewp_render_theme_icon($icon_slug, $class_name = '', $provider = 'lucide') {
630
+ $icon_slug = is_string($icon_slug) ? sanitize_title($icon_slug) : '';
631
+ if ($icon_slug === '') {
632
+ return;
633
+ }
634
+
635
+ $svgs = array(
636
+ ${iconSvgMapPhp}
637
+ );
638
+
639
+ $custom_svgs = get_option('forgewp_custom_icons', array());
640
+ $svg = '';
641
+
642
+ if ($provider === 'custom' && isset($custom_svgs[$icon_slug])) {
643
+ $svg = $custom_svgs[$icon_slug];
644
+ } elseif (isset($svgs[$icon_slug])) {
645
+ $svg = $svgs[$icon_slug];
646
+ } elseif (isset($custom_svgs[$icon_slug])) {
647
+ $svg = $custom_svgs[$icon_slug];
648
+ }
649
+
650
+ if (!empty($svg)) {
651
+ if (!empty($class_name)) {
652
+ if (preg_match('/\\sclass="/', $svg)) {
653
+ $svg = preg_replace('/\\sclass="([^"]*)"/', ' class="$1 ' . esc_attr($class_name) . '"', $svg, 1);
654
+ } else {
655
+ $svg = str_replace('<svg ', '<svg class="' . esc_attr($class_name) . '" ', $svg);
656
+ }
657
+ }
658
+ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted inline SVG registry
659
+ echo $svg;
660
+ return;
661
+ }
662
+
663
+ // Dashicons fallback (core admin font; also enqueued on frontend if theme uses dashicons)
664
+ $dash = preg_replace('/^dashicons-/', '', $icon_slug);
665
+ echo '<span class="dashicons dashicons-' . esc_attr($dash) . ( $class_name ? ' ' . esc_attr($class_name) : '' ) . '" aria-hidden="true"></span>';
666
+ }
667
+
668
+ /**
669
+ * Prevent TinyMCE Classic Editor auto-resize height expansion feedback loop.
670
+ */
671
+ function forgewp_disable_editor_autoresize($init_array) {
672
+ if (isset($init_array['plugins'])) {
673
+ $plugins = explode(',', $init_array['plugins']);
674
+ $key = array_search('wpautoresize', $plugins);
675
+ if ($key !== false) {
676
+ unset($plugins[$key]);
677
+ $init_array['plugins'] = implode(',', $plugins);
678
+ }
679
+ }
680
+ return $init_array;
681
+ }
682
+ add_filter('tiny_mce_before_init', 'forgewp_disable_editor_autoresize');
683
+
684
+ /**
685
+ * Ensure dedicated page-[slug].php and single-[post_type].php templates load if they exist in the theme,
686
+ * overriding stale 'page-placeholder.php' assignments or WooCommerce default fallback hooks.
687
+ * Also resolves SPA sub-routes (e.g. /account/*) so direct visits never 404.
688
+ */
689
+ function forgewp_route_template_include(string $template): string {
690
+ if (is_singular()) {
691
+ $post_type = get_post_type();
692
+ if ($post_type) {
693
+ $single_template = get_template_directory() . '/single-' . $post_type . '.php';
694
+ if (file_exists($single_template)) {
695
+ return $single_template;
696
+ }
697
+ }
698
+ }
699
+ if (is_page()) {
700
+ $post = get_queried_object();
701
+ if ($post instanceof WP_Post) {
702
+ $slug_template = get_template_directory() . '/page-' . $post->post_name . '.php';
703
+ if (file_exists($slug_template)) {
704
+ $current_meta = get_post_meta($post->ID, '_wp_page_template', true);
705
+ if (empty($current_meta) || $current_meta === 'page-placeholder.php' || $current_meta === 'default') {
706
+ return $slug_template;
707
+ }
708
+ }
709
+ }
710
+ }
711
+
712
+ // SPA sub-route and 404 fallback resolution
713
+ $req_uri = $_SERVER['REQUEST_URI'] ?? '';
714
+ $parsed_path = trim((string)parse_url($req_uri, PHP_URL_PATH), '/');
715
+
716
+ // Strip WordPress install subfolder if present
717
+ $site_path = trim((string)parse_url(home_url(), PHP_URL_PATH), '/');
718
+ if ($site_path !== '' && strpos($parsed_path, $site_path) === 0) {
719
+ $parsed_path = trim(substr($parsed_path, strlen($site_path)), '/');
720
+ }
721
+
722
+ if ($parsed_path !== '') {
723
+ $dir = get_template_directory();
724
+
725
+ // 1. Direct flat template match (e.g. account/orders -> page-account-orders.php)
726
+ $flat_slug = str_replace('/', '-', $parsed_path);
727
+ $flat_tpl = "{$dir}/page-{$flat_slug}.php";
728
+ if (file_exists($flat_tpl)) {
729
+ global $wp_query;
730
+ if ($wp_query && $wp_query->is_404) {
731
+ $wp_query->is_404 = false;
732
+ $wp_query->is_page = true;
733
+ status_header(200);
734
+ }
735
+ return $flat_tpl;
736
+ }
737
+
738
+ // 2. Parent section template match (e.g. account/orders/123 -> page-account.php)
739
+ $parts = explode('/', $parsed_path);
740
+ while (count($parts) > 1) {
741
+ array_pop($parts);
742
+ $parent_slug = implode('-', $parts);
743
+ $parent_tpl = "{$dir}/page-{$parent_slug}.php";
744
+ if (file_exists($parent_tpl)) {
745
+ global $wp_query;
746
+ if ($wp_query && $wp_query->is_404) {
747
+ $wp_query->is_404 = false;
748
+ $wp_query->is_page = true;
749
+ status_header(200);
750
+ }
751
+ return $parent_tpl;
752
+ }
753
+ }
754
+ }
755
+
756
+ return $template;
757
+ }
758
+ add_filter('template_include', 'forgewp_route_template_include', 99);
759
+ `;
760
+
761
+ return `
762
+ ${autoCreationPhp}
763
+ ${templateResolutionsPhp}
764
+ `;
765
+ }