@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,273 @@
1
+ /**
2
+ * Generates a WordPress Admin settings page for all site options detected
3
+ * via useWpOption() calls in the theme source.
4
+ *
5
+ * Options annotated with { postType: '...' } get a Select2 AJAX-powered
6
+ * post picker instead of a plain text input — scales to thousands of posts.
7
+ *
8
+ * @param {Array<{name: string, label: string, defaultValue: string, postType?: string|null}>} wpOptions
9
+ * @param {{textDomain: string}} config
10
+ * @returns {string} PHP code to append to functions.php
11
+ */
12
+ export function buildSettingsPagePhp(wpOptions, config) {
13
+ if (!wpOptions || wpOptions.length === 0) return '';
14
+
15
+ const textDomain = config.textDomain || 'forgewp';
16
+ const fnPrefix = textDomain.replace(/-/g, '_');
17
+ const pageSlug = `${textDomain}-theme-options`;
18
+ const optionGroup = `${fnPrefix}_theme_options`;
19
+ const capability = 'manage_options';
20
+ const sectionId = `${fnPrefix}_main_section`;
21
+ const ajaxAction = `${fnPrefix}_search_posts`;
22
+ const nonceName = `${fnPrefix}_search_posts_nonce`;
23
+
24
+ const hasPostPicker = wpOptions.some(o => o.postType);
25
+
26
+ // ── register_setting() calls ───────────────────────────────────────────────
27
+ const registerCalls = wpOptions.map(({ name, label, defaultValue, postType }) => {
28
+ // Post-picker fields store a post ID; sanitize as absint-compatible integer string
29
+ const sanitize = postType
30
+ ? 'absint'
31
+ : (name.includes('url') || name.includes('link') ? 'esc_url_raw' : 'sanitize_text_field');
32
+ return `
33
+ register_setting('${optionGroup}', '${name}', array(
34
+ 'type' => 'string',
35
+ 'sanitize_callback' => '${sanitize}',
36
+ 'default' => '${(defaultValue || '').replace(/'/g, "\\'")}',
37
+ ));`;
38
+ }).join('\n');
39
+
40
+ // ── add_settings_field() calls ─────────────────────────────────────────────
41
+ const fieldCalls = wpOptions.map(({ name, label }) => {
42
+ const escapedLabel = (label || name).replace(/'/g, "\\'");
43
+ return `
44
+ add_settings_field(
45
+ '${name}',
46
+ __('${escapedLabel}', '${textDomain}'),
47
+ '${fnPrefix}_render_field_${name}',
48
+ '${pageSlug}',
49
+ '${sectionId}'
50
+ );`;
51
+ }).join('\n');
52
+
53
+ // ── Individual field render callbacks ─────────────────────────────────────
54
+ const fieldCallbacks = wpOptions.map(({ name, label, defaultValue, postType }) => {
55
+ const escapedLabel = (label || name).replace(/'/g, "\\'");
56
+ const escapedDefault = (defaultValue || '').replace(/'/g, "\\'");
57
+
58
+ if (postType) {
59
+ // Select2-powered post picker
60
+ return `
61
+ function ${fnPrefix}_render_field_${name}() {
62
+ $value = get_option('${name}', '${escapedDefault}');
63
+ $selected_title = '';
64
+ if ($value && intval($value) > 0) {
65
+ $selected_post = get_post(intval($value));
66
+ $selected_title = $selected_post ? $selected_post->post_title . ' (ID: ' . $value . ')' : '';
67
+ }
68
+ ?>
69
+ <select id="<?php echo esc_attr('${name}'); ?>"
70
+ name="<?php echo esc_attr('${name}'); ?>"
71
+ class="forgewp-post-select"
72
+ data-post-type="<?php echo esc_attr('${postType}'); ?>"
73
+ data-nonce="<?php echo esc_attr(wp_create_nonce('${nonceName}')); ?>"
74
+ style="min-width:400px">
75
+ <?php if ($value && $selected_title) : ?>
76
+ <option value="<?php echo esc_attr($value); ?>" selected>
77
+ <?php echo esc_html($selected_title); ?>
78
+ </option>
79
+ <?php else : ?>
80
+ <option value=""><?php _e('— Select a post —', '${textDomain}'); ?></option>
81
+ <?php endif; ?>
82
+ </select>
83
+ <p class="description"><?php _e('${escapedLabel}', '${textDomain}'); ?></p>
84
+ <?php
85
+ }`;
86
+ }
87
+
88
+ // Plain text input
89
+ return `
90
+ function ${fnPrefix}_render_field_${name}() {
91
+ $value = get_option('${name}', '${escapedDefault}');
92
+ echo '<input type="text" id="${name}" name="${name}" value="' . esc_attr($value) . '" class="regular-text" />';
93
+ echo '<p class="description">${escapedLabel}</p>';
94
+ }`;
95
+ }).join('\n');
96
+
97
+ // ── Select2 AJAX search handler (only emitted when there are postType options) ──
98
+ const ajaxHandler = hasPostPicker ? `
99
+ /**
100
+ * ForgeWP AJAX: search posts by type for the Theme Options post picker.
101
+ */
102
+ if (!function_exists('${fnPrefix}_ajax_search_posts')) {
103
+ function ${fnPrefix}_ajax_search_posts() {
104
+ check_ajax_referer('${nonceName}', 'nonce');
105
+ if (!current_user_can('${capability}')) {
106
+ wp_send_json_error('Unauthorized', 403);
107
+ }
108
+ $post_type = isset($_GET['post_type']) ? sanitize_key($_GET['post_type']) : 'post';
109
+ $search = isset($_GET['search']) ? sanitize_text_field($_GET['search']) : '';
110
+ $results = array();
111
+ $query = new WP_Query(array(
112
+ 'post_type' => $post_type,
113
+ 'post_status' => 'publish',
114
+ 's' => $search,
115
+ 'posts_per_page' => 40,
116
+ 'orderby' => 'title',
117
+ 'order' => 'ASC',
118
+ 'no_found_rows' => true,
119
+ ));
120
+ foreach ($query->posts as $post) {
121
+ $results[] = array(
122
+ 'id' => $post->ID,
123
+ 'text' => $post->post_title . ' (ID: ' . $post->ID . ')',
124
+ );
125
+ }
126
+ wp_send_json_success($results);
127
+ }
128
+ add_action('wp_ajax_${ajaxAction}', '${fnPrefix}_ajax_search_posts');
129
+ }
130
+ ` : '';
131
+
132
+ // ── Admin script enqueue (Select2 init + AJAX wiring) ─────────────────────
133
+ // WordPress core does NOT ship a public 'select2' script/style handle — calling
134
+ // wp_enqueue_script('select2') silently no-ops against an unregistered handle, which
135
+ // means the library never loads and the field falls back to a plain <select> containing
136
+ // only the single server-rendered <option> for the currently-stored value. We vendor our
137
+ // own copy of Select2 into the exported theme (see generate-theme.js) and enqueue that.
138
+ const select2Handle = `${fnPrefix}-select2`;
139
+ const enqueueScript = hasPostPicker ? `
140
+ if (!function_exists('${fnPrefix}_enqueue_options_page_assets')) {
141
+ function ${fnPrefix}_enqueue_options_page_assets($hook) {
142
+ // Only load on our settings page
143
+ if ($hook !== 'appearance_page_${pageSlug}') return;
144
+
145
+ $select2_js = get_template_directory() . '/assets/vendor/select2/select2.min.js';
146
+ $select2_css = get_template_directory() . '/assets/vendor/select2/select2.min.css';
147
+ if (!file_exists($select2_js)) return;
148
+
149
+ wp_enqueue_script(
150
+ '${select2Handle}',
151
+ get_template_directory_uri() . '/assets/vendor/select2/select2.min.js',
152
+ array('jquery'),
153
+ FORGEWP_THEME_VERSION,
154
+ true
155
+ );
156
+ if (file_exists($select2_css)) {
157
+ wp_enqueue_style(
158
+ '${select2Handle}',
159
+ get_template_directory_uri() . '/assets/vendor/select2/select2.min.css',
160
+ array(),
161
+ FORGEWP_THEME_VERSION
162
+ );
163
+ }
164
+
165
+ // Inline initialisation script
166
+ $init_js = "
167
+ jQuery(function($) {
168
+ $('.forgewp-post-select').each(function() {
169
+ var \\$select = $(this);
170
+ var postType = \\$select.data('post-type');
171
+ var nonce = \\$select.data('nonce');
172
+ var ajaxUrl = " . json_encode(admin_url('admin-ajax.php')) . ";
173
+ \\$select.select2({
174
+ placeholder: '— Type to search —',
175
+ allowClear: true,
176
+ minimumInputLength: 0,
177
+ ajax: {
178
+ url: ajaxUrl,
179
+ dataType: 'json',
180
+ delay: 250,
181
+ data: function(params) {
182
+ return {
183
+ action: '${ajaxAction}',
184
+ nonce: nonce,
185
+ post_type: postType,
186
+ search: params.term || '',
187
+ };
188
+ },
189
+ processResults: function(data) {
190
+ if (!data.success) return { results: [] };
191
+ return { results: data.data };
192
+ },
193
+ cache: true,
194
+ },
195
+ });
196
+ });
197
+ });
198
+ ";
199
+ wp_add_inline_script('${select2Handle}', $init_js);
200
+ }
201
+ add_action('admin_enqueue_scripts', '${fnPrefix}_enqueue_options_page_assets');
202
+ }
203
+ ` : '';
204
+
205
+ return `
206
+ /**
207
+ * ForgeWP Theme Options — Auto-generated from useWpOption() calls detected in source.
208
+ * Manage these at Appearance → Theme Options in the WordPress admin.
209
+ *
210
+ * Options with a postType annotation use a Select2 AJAX-powered post picker
211
+ * that scales to any number of posts without performance issues.
212
+ */
213
+
214
+ ${ajaxHandler}
215
+ ${enqueueScript}
216
+
217
+ if (!function_exists('${fnPrefix}_register_settings')) {
218
+ function ${fnPrefix}_register_settings() {
219
+ ${registerCalls}
220
+
221
+ add_settings_section(
222
+ '${sectionId}',
223
+ __('Theme Options', '${textDomain}'),
224
+ '__return_false',
225
+ '${pageSlug}'
226
+ );
227
+
228
+ ${fieldCalls}
229
+ }
230
+ add_action('admin_init', '${fnPrefix}_register_settings');
231
+ }
232
+
233
+ if (!function_exists('${fnPrefix}_add_options_page')) {
234
+ function ${fnPrefix}_add_options_page() {
235
+ add_theme_page(
236
+ __('Theme Options', '${textDomain}'),
237
+ __('Theme Options', '${textDomain}'),
238
+ '${capability}',
239
+ '${pageSlug}',
240
+ '${fnPrefix}_render_options_page'
241
+ );
242
+ }
243
+ add_action('admin_menu', '${fnPrefix}_add_options_page');
244
+ }
245
+
246
+ if (!function_exists('${fnPrefix}_render_options_page')) {
247
+ function ${fnPrefix}_render_options_page() {
248
+ if (!current_user_can('${capability}')) {
249
+ wp_die(__('You do not have permission to access this page.', '${textDomain}'));
250
+ }
251
+ ?>
252
+ <div class="wrap">
253
+ <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
254
+ <?php if (isset($_GET['settings-updated'])) : ?>
255
+ <div id="message" class="notice notice-success is-dismissible">
256
+ <p><?php _e('Settings saved.', '${textDomain}'); ?></p>
257
+ </div>
258
+ <?php endif; ?>
259
+ <form method="post" action="options.php">
260
+ <?php
261
+ settings_fields('${optionGroup}');
262
+ do_settings_sections('${pageSlug}');
263
+ submit_button(__('Save Settings', '${textDomain}'));
264
+ ?>
265
+ </form>
266
+ </div>
267
+ <?php
268
+ }
269
+ }
270
+
271
+ ${fieldCallbacks}
272
+ `;
273
+ }