@forgewp/compiler 0.4.2 → 0.4.3
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.
- package/README.md +11 -3
- package/bin/add.js +15 -6
- package/bin/analyze.js +66 -1
- package/bin/cli.js +16 -12
- package/bin/fresh.js +1 -1
- package/bin/make-block.js +11 -0
- package/bin/make-island.js +11 -0
- package/bin/{make-template.js → make-loop.js} +24 -19
- package/bin/make-page.js +83 -0
- package/bin/make-post-type.js +11 -0
- package/bin/make-sandbox.js +558 -0
- package/lib/adapters/html-adapter.js +8 -3
- package/lib/adapters/stub-adapter.js +6 -2
- package/lib/asset-loader.js +22 -0
- package/lib/blueprints.js +133 -1
- package/lib/functions-builder.js +929 -3
- package/lib/generate-theme.js +219 -23
- package/lib/hydration-scanner.js +69 -57
- package/lib/index.d.ts +38 -1
- package/lib/index.js +1 -1
- package/lib/load-config.js +98 -0
- package/lib/markup-processor.js +105 -0
- package/lib/page-config-plugin.js +40 -0
- package/lib/php-builders.js +4 -2
- package/lib/react-adapter.js +82 -23
- package/lib/render-theme.mts +97 -24
- package/lib/validate-export.js +49 -0
- package/package.json +2 -1
- package/templates/PresetsStyle.tsx +1 -1
- package/templates/auth/components/dashboard-view-wrapper.tsx +168 -0
- package/templates/auth/components/forgot-password-form-wrapper.tsx +133 -0
- package/templates/auth/components/login-form-wrapper.tsx +203 -0
- package/templates/auth/components/reset-password-form-wrapper.tsx +182 -0
- package/templates/auth/components/signup-form-wrapper.tsx +257 -0
- package/templates/auth/components/verify-email-view-wrapper.tsx +265 -0
- package/templates/auth/pages/DashboardPage.tsx +15 -0
- package/templates/auth/pages/ForgotPasswordPage.tsx +14 -0
- package/templates/auth/pages/LoginPage.tsx +14 -0
- package/templates/auth/pages/ResetPasswordPage.tsx +14 -0
- package/templates/auth/pages/SignUpPage.tsx +14 -0
- package/templates/auth/pages/VerifyEmailPage.tsx +14 -0
- package/templates/forgewp-config.ts +70 -3
- package/templates/mock-data.json +122 -433
- package/templates/products.json +5 -0
- package/templates/roles.json +7 -0
- package/templates/sessions.json +1 -0
- package/templates/users.json +34 -0
- package/templates/wordpress.tsx +1390 -1297
package/lib/markup-processor.js
CHANGED
|
@@ -256,6 +256,8 @@ if (function_exists('pll_the_languages')) {
|
|
|
256
256
|
getAttr('postsPerPage') || getAttr('postsperpage') || '10';
|
|
257
257
|
const categoryName =
|
|
258
258
|
getAttr('categoryName') || getAttr('categoryname') || '';
|
|
259
|
+
const orderby = getAttr('orderby') || '';
|
|
260
|
+
const order = getAttr('order') || '';
|
|
259
261
|
const metaKey = getAttr('metaKey') || getAttr('metakey') || '';
|
|
260
262
|
const metaValue = getAttr('metaValue') || getAttr('metavalue') || '';
|
|
261
263
|
const metaCompare = getAttr('metaCompare') || getAttr('metacompare') || '=';
|
|
@@ -283,6 +285,12 @@ if (function_exists('pll_the_languages')) {
|
|
|
283
285
|
|
|
284
286
|
let phpArgs = ` 'post_type' => '${postType}',\n 'posts_per_page' => ${postsPerPage},`;
|
|
285
287
|
|
|
288
|
+
if (orderby !== '') {
|
|
289
|
+
phpArgs += `\n 'orderby' => '${orderby}',`;
|
|
290
|
+
}
|
|
291
|
+
if (order !== '') {
|
|
292
|
+
phpArgs += `\n 'order' => '${order}',`;
|
|
293
|
+
}
|
|
286
294
|
if (categoryName !== '') {
|
|
287
295
|
phpArgs += `\n 'category_name' => '${categoryName}',`;
|
|
288
296
|
}
|
|
@@ -424,6 +432,44 @@ ${phpArgs}
|
|
|
424
432
|
);
|
|
425
433
|
processed = processed.replace(/<\/forgewp-icon-placeholder>/g, '');
|
|
426
434
|
|
|
435
|
+
// ── Authentication & Capability Gates ──
|
|
436
|
+
processed = processed.replace(
|
|
437
|
+
/<forgewp-auth-gate-start\s*\/?>/g,
|
|
438
|
+
'<?php if ( is_user_logged_in() ) : ?>',
|
|
439
|
+
);
|
|
440
|
+
processed = processed.replace(/<\/forgewp-auth-gate-start>/g, '');
|
|
441
|
+
|
|
442
|
+
processed = processed.replace(
|
|
443
|
+
/<forgewp-auth-gate-fallback\s*\/?>/g,
|
|
444
|
+
'<?php else : ?>',
|
|
445
|
+
);
|
|
446
|
+
processed = processed.replace(/<\/forgewp-auth-gate-fallback>/g, '');
|
|
447
|
+
|
|
448
|
+
processed = processed.replace(
|
|
449
|
+
/<forgewp-auth-gate-end\s*\/?>/g,
|
|
450
|
+
'<?php endif; ?>',
|
|
451
|
+
);
|
|
452
|
+
processed = processed.replace(/<\/forgewp-auth-gate-end>/g, '');
|
|
453
|
+
|
|
454
|
+
processed = processed.replace(
|
|
455
|
+
/<forgewp-capability-gate-start\s+[^>]*allowed="([^"]+)"\s*\/?>/g,
|
|
456
|
+
"<?php if ( current_user_can( '$1' ) ) : ?>",
|
|
457
|
+
);
|
|
458
|
+
processed = processed.replace(/<\/forgewp-capability-gate-start>/g, '');
|
|
459
|
+
|
|
460
|
+
processed = processed.replace(
|
|
461
|
+
/<forgewp-capability-gate-fallback\s*\/?>/g,
|
|
462
|
+
'<?php else : ?>',
|
|
463
|
+
);
|
|
464
|
+
processed = processed.replace(/<\/forgewp-capability-gate-fallback>/g, '');
|
|
465
|
+
|
|
466
|
+
processed = processed.replace(
|
|
467
|
+
/<forgewp-capability-gate-end\s*\/?>/g,
|
|
468
|
+
'<?php endif; ?>',
|
|
469
|
+
);
|
|
470
|
+
processed = processed.replace(/<\/forgewp-capability-gate-end>/g, '');
|
|
471
|
+
|
|
472
|
+
|
|
427
473
|
processed = processed.replace(
|
|
428
474
|
/__FORGEWP_REPEATER_FIELD_([a-zA-Z0-9_-]+)__/g,
|
|
429
475
|
(match, fieldName) => `<?php echo esc_html($row['${fieldName}']); ?>`
|
|
@@ -565,6 +611,65 @@ ${phpArgs}
|
|
|
565
611
|
"<?php echo esc_url( get_template_directory_uri() ); ?>",
|
|
566
612
|
);
|
|
567
613
|
|
|
614
|
+
// ── WooCommerce Product Loop Blocks ──
|
|
615
|
+
processed = processed.replace(
|
|
616
|
+
/<forgewp-product-loop-start\s+([^>]*)\/?>/g,
|
|
617
|
+
(match, attrsStr) => {
|
|
618
|
+
const getAttr = (name) => {
|
|
619
|
+
const regex = new RegExp(
|
|
620
|
+
`(?:${name}|${name.toLowerCase()})=(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`,
|
|
621
|
+
'i',
|
|
622
|
+
);
|
|
623
|
+
const m = attrsStr.match(regex);
|
|
624
|
+
return m ? m[1] || m[2] || m[3] || '' : '';
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
const category = getAttr('category') || '';
|
|
628
|
+
const limit = getAttr('limit') || '10';
|
|
629
|
+
const orderBy = getAttr('orderBy') || getAttr('orderby') || 'date';
|
|
630
|
+
const order = getAttr('order') || 'desc';
|
|
631
|
+
const status = getAttr('status') || 'publish';
|
|
632
|
+
|
|
633
|
+
let catQuery = '';
|
|
634
|
+
if (category !== '') {
|
|
635
|
+
catQuery = `\n 'category' => array('${category}'),`;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
return `<?php
|
|
639
|
+
$query_args = array(
|
|
640
|
+
'limit' => ${limit},
|
|
641
|
+
'orderby' => '${orderBy}',
|
|
642
|
+
'order' => '${order}',
|
|
643
|
+
'status' => '${status}',${catQuery}
|
|
644
|
+
);
|
|
645
|
+
$products = wc_get_products($query_args);
|
|
646
|
+
foreach ($products as $product) {
|
|
647
|
+
global $product;
|
|
648
|
+
setup_postdata(get_the_ID());
|
|
649
|
+
?>`;
|
|
650
|
+
},
|
|
651
|
+
);
|
|
652
|
+
processed = processed.replace(/<\/forgewp-product-loop-start>/g, '');
|
|
653
|
+
processed = processed.replace(
|
|
654
|
+
/<forgewp-product-loop-end\s*\/?>/g,
|
|
655
|
+
'<?php\n }\n wp_reset_postdata();\n ?>',
|
|
656
|
+
);
|
|
657
|
+
processed = processed.replace(/<\/forgewp-product-loop-end>/g, '');
|
|
658
|
+
|
|
659
|
+
// ── WooCommerce Dynamic Hook Replacements ──
|
|
660
|
+
processed = processed.replace(
|
|
661
|
+
/__FORGEWP_PRODUCT_PRICE__/g,
|
|
662
|
+
'<?php global $product; echo wp_kses_post( $product->get_price_html() ); ?>'
|
|
663
|
+
);
|
|
664
|
+
processed = processed.replace(
|
|
665
|
+
/__FORGEWP_PRODUCT_SKU__/g,
|
|
666
|
+
'<?php global $product; echo esc_html( $product->get_sku() ); ?>'
|
|
667
|
+
);
|
|
668
|
+
processed = processed.replace(
|
|
669
|
+
/__FORGEWP_PRODUCT_RATING__/g,
|
|
670
|
+
'<?php global $product; echo wp_kses_post( wc_get_rating_html( $product->get_average_rating() ) ); ?>'
|
|
671
|
+
);
|
|
672
|
+
|
|
568
673
|
return processed;
|
|
569
674
|
}
|
|
570
675
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom Vite Plugin to statically parse "export const page = { ... }" configs in pages,
|
|
5
|
+
* and dynamically inject useWpPageProtect(page) hook call at component startup.
|
|
6
|
+
*/
|
|
7
|
+
export function forgewpPageConfigPlugin() {
|
|
8
|
+
return {
|
|
9
|
+
name: 'forgewp-page-config',
|
|
10
|
+
transform(code, id) {
|
|
11
|
+
// Check if it's a page component inside src/app/pages/
|
|
12
|
+
const normalizedPath = id.replace(/\\/g, '/');
|
|
13
|
+
if (!normalizedPath.includes('/src/app/pages/') || !normalizedPath.endsWith('.tsx')) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (!code.includes('export const pageConfig')) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Prepend hook import
|
|
21
|
+
let transformed = `import { useWpPageProtect as _useWpPageProtect } from '@forgewp/auth';\n` + code;
|
|
22
|
+
|
|
23
|
+
// Match functional components
|
|
24
|
+
const funcRegex = /export\s+(default\s+)?function\s+([a-zA-Z0-9_$]+)\s*\(([\s\S]*?)\)\s*\{/g;
|
|
25
|
+
if (funcRegex.test(transformed)) {
|
|
26
|
+
transformed = transformed.replace(funcRegex, `export $1function $2($3) {\n _useWpPageProtect(pageConfig);`);
|
|
27
|
+
return { code: transformed, map: null };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Match arrow function component exports
|
|
31
|
+
const arrowRegex = /export\s+(const|let|var)\s+([a-zA-Z0-9_$]+)(\s*:\s*[a-zA-Z0-9_$.<>]+)?\s*=\s*\(([\s\S]*?)\)\s*=>\s*\{/g;
|
|
32
|
+
if (arrowRegex.test(transformed)) {
|
|
33
|
+
transformed = transformed.replace(arrowRegex, `export $1 $2$3 = ($4) => {\n _useWpPageProtect(pageConfig);`);
|
|
34
|
+
return { code: transformed, map: null };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
package/lib/php-builders.js
CHANGED
|
@@ -171,8 +171,10 @@ export function buildHeaderPhp(config) {
|
|
|
171
171
|
<?php wp_head(); ?>
|
|
172
172
|
<style>
|
|
173
173
|
/* Reset WP link underlines to match React/Tailwind expected styling */
|
|
174
|
-
a { text-decoration: none
|
|
175
|
-
a:hover { text-decoration: none
|
|
174
|
+
a { text-decoration: none; }
|
|
175
|
+
a:hover { text-decoration: none; }
|
|
176
|
+
.underline { text-decoration: underline !important; }
|
|
177
|
+
.hover\\:underline:hover { text-decoration: underline !important; }
|
|
176
178
|
</style>
|
|
177
179
|
</head>
|
|
178
180
|
<body <?php body_class(); ?>>
|
package/lib/react-adapter.js
CHANGED
|
@@ -799,15 +799,15 @@ ${attributesRegistry}
|
|
|
799
799
|
console.log(`\n🎉 Run ${pc.cyan('pnpm export')} to automatically register it inside your WordPress theme!\n`);
|
|
800
800
|
}
|
|
801
801
|
|
|
802
|
-
export function
|
|
803
|
-
const
|
|
804
|
-
if (!existsSync(
|
|
805
|
-
mkdirSync(
|
|
802
|
+
export function onMakeLoop(themeRoot, { pascalCase, postType, customFields, automaticallySeeded, pc }) {
|
|
803
|
+
const loopsDir = path.join(themeRoot, 'src', 'loops');
|
|
804
|
+
if (!existsSync(loopsDir)) {
|
|
805
|
+
mkdirSync(loopsDir, { recursive: true });
|
|
806
806
|
}
|
|
807
807
|
|
|
808
|
-
const targetFile = path.join(
|
|
808
|
+
const targetFile = path.join(loopsDir, `${pascalCase}.tsx`);
|
|
809
809
|
if (existsSync(targetFile)) {
|
|
810
|
-
console.error(pc.red(`\n❌ Error:
|
|
810
|
+
console.error(pc.red(`\n❌ Error: Loop "${pascalCase}.tsx" already exists at src/loops/\n`));
|
|
811
811
|
process.exit(1);
|
|
812
812
|
}
|
|
813
813
|
|
|
@@ -828,12 +828,12 @@ export function onMakeTemplate(themeRoot, { pascalCase, postType, customFields,
|
|
|
828
828
|
customFieldsMarkup = `{/* No custom fields registered in mock-data.json. Add fields by running forgewp make:post-type */}\n <p className="text-xs text-zinc-500 font-mono italic">No custom fields configured for post type: ${postType}</p>`;
|
|
829
829
|
}
|
|
830
830
|
|
|
831
|
-
const
|
|
831
|
+
const loopTemplate = `import { WpQueryLoop, useWpTitle, useWpFeaturedImage, useWpExcerpt, useWpPermalink, useWpCustomField } from "@/.forgewp/wordpress";
|
|
832
832
|
|
|
833
833
|
/**
|
|
834
|
-
* ⚡ ForgeWP Custom Post Type
|
|
834
|
+
* ⚡ ForgeWP Custom Post Type Loop — "${pascalCase}"
|
|
835
835
|
*
|
|
836
|
-
* This loop
|
|
836
|
+
* This loop dynamically queries and renders records of the "${postType}" post type.
|
|
837
837
|
* In local development, the post data and custom fields are fetched dynamically from
|
|
838
838
|
* your local JSON database file at: \`cms/mock-data.json\`.
|
|
839
839
|
*/
|
|
@@ -844,13 +844,13 @@ export default function ${pascalCase}() {
|
|
|
844
844
|
{/* Header Banner */}
|
|
845
845
|
<div className="mb-12 border border-slate-100 bg-white p-8 shadow-xl shadow-slate-100/50">
|
|
846
846
|
<span className="inline-block bg-primary/5 text-primary border border-primary/10 text-xs font-mono font-bold uppercase tracking-wider px-3 py-1 mb-4">
|
|
847
|
-
Loop
|
|
847
|
+
Post Loop
|
|
848
848
|
</span>
|
|
849
849
|
<h1 className="text-4xl md:text-5xl font-black uppercase tracking-tight text-slate-900">
|
|
850
850
|
Latest ${postType.charAt(0).toUpperCase() + postType.slice(1)} Feed
|
|
851
851
|
</h1>
|
|
852
852
|
<p className="text-sm font-mono font-medium text-slate-500 mt-2">
|
|
853
|
-
Dynamic Post-Type
|
|
853
|
+
Dynamic Post-Type Loop • Querying: "${postType}"
|
|
854
854
|
</p>
|
|
855
855
|
</div>
|
|
856
856
|
|
|
@@ -886,26 +886,21 @@ export default function ${pascalCase}() {
|
|
|
886
886
|
}
|
|
887
887
|
`;
|
|
888
888
|
|
|
889
|
-
writeFileSync(targetFile,
|
|
889
|
+
writeFileSync(targetFile, loopTemplate, 'utf8');
|
|
890
890
|
|
|
891
891
|
if (automaticallySeeded) {
|
|
892
892
|
console.log(pc.yellow(`\n⚠️ Post type "${postType}" was missing from local database (mock-data.json).`));
|
|
893
893
|
console.log(` We have automatically registered it and seeded default custom fields for you!`);
|
|
894
894
|
}
|
|
895
895
|
|
|
896
|
-
console.log(pc.green(`\n⚡ Loop
|
|
897
|
-
console.log(` Location: ${pc.cyan(`src/
|
|
896
|
+
console.log(pc.green(`\n⚡ Post Loop "${pascalCase}" successfully created!`));
|
|
897
|
+
console.log(` Location: ${pc.cyan(`src/loops/${pascalCase}.tsx`)}`);
|
|
898
898
|
console.log(` Target Post Type: ${pc.yellow(postType)}`);
|
|
899
899
|
if (customFields.length > 0) {
|
|
900
900
|
console.log(` Seeded Custom Fields: ${pc.yellow(customFields.join(', '))}`);
|
|
901
901
|
}
|
|
902
902
|
console.log(`\n🎉 You can now import it directly inside your pages:`);
|
|
903
|
-
console.log(` ${pc.cyan(`import ${pascalCase} from "@/
|
|
904
|
-
}
|
|
905
|
-
|
|
906
|
-
// Backward compatible proxy forwarding make:component parameters straight to onMakeTemplate
|
|
907
|
-
export function onMakeComponent(themeRoot, options) {
|
|
908
|
-
return onMakeTemplate(themeRoot, options);
|
|
903
|
+
console.log(` ${pc.cyan(`import ${pascalCase} from "@/loops/${pascalCase}";`)}\n`);
|
|
909
904
|
}
|
|
910
905
|
|
|
911
906
|
export function onMakeIsland(themeRoot, { pascalCase, pc }) {
|
|
@@ -990,6 +985,67 @@ export default function ${pascalCase}({ label = "React State Island (${pascalCas
|
|
|
990
985
|
console.log(` ${pc.cyan(`<Hydrate trigger="visible" preload="near-visible">\n <${pascalCase} />\n </Hydrate>`)}\n`);
|
|
991
986
|
}
|
|
992
987
|
|
|
988
|
+
export function onMakePage(themeRoot, { pascalCase, pc }) {
|
|
989
|
+
const pagesDir = path.join(themeRoot, 'src', 'app', 'pages');
|
|
990
|
+
if (!existsSync(pagesDir)) {
|
|
991
|
+
mkdirSync(pagesDir, { recursive: true });
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
const targetFile = path.join(pagesDir, `${pascalCase}.tsx`);
|
|
995
|
+
if (existsSync(targetFile)) {
|
|
996
|
+
console.error(pc.red(`\n❌ Error: Page Template "${pascalCase}.tsx" already exists at src/app/pages/\n`));
|
|
997
|
+
process.exit(1);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
const pageTemplate = `import { WpHead, useWpTitle, useWpExcerpt } from "../../.forgewp/wordpress";
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* ⚡ ForgeWP Custom Page Template — "${pascalCase}"
|
|
1004
|
+
*
|
|
1005
|
+
* This file compiles into a standalone WordPress Custom Page Template.
|
|
1006
|
+
* You can select it in the WordPress page editor dropdown as "Template: ${pascalCase}".
|
|
1007
|
+
*/
|
|
1008
|
+
export function ${pascalCase}() {
|
|
1009
|
+
return (
|
|
1010
|
+
<main className="container mx-auto px-6 py-12 font-sans text-slate-900">
|
|
1011
|
+
<WpHead
|
|
1012
|
+
title="${pascalCase}"
|
|
1013
|
+
description="Dynamic WordPress Custom Page Template ${pascalCase} compiled with ForgeWP."
|
|
1014
|
+
/>
|
|
1015
|
+
|
|
1016
|
+
{/* Hero Header Area */}
|
|
1017
|
+
<header className="border-b border-slate-100 pb-6 mb-12">
|
|
1018
|
+
<span className="inline-block bg-primary/5 text-primary border border-primary/10 text-xs font-mono font-bold uppercase tracking-wider px-3 py-1 mb-4">
|
|
1019
|
+
Page Template
|
|
1020
|
+
</span>
|
|
1021
|
+
<h1 className="text-5xl font-black tracking-tight uppercase text-slate-900">
|
|
1022
|
+
{useWpTitle() || "${pascalCase}"}
|
|
1023
|
+
</h1>
|
|
1024
|
+
<p className="text-slate-500 mt-2 text-sm leading-relaxed">
|
|
1025
|
+
{useWpExcerpt() || "Custom page layout designed dynamically."}
|
|
1026
|
+
</p>
|
|
1027
|
+
</header>
|
|
1028
|
+
|
|
1029
|
+
{/* Page Content layout */}
|
|
1030
|
+
<div className="bg-white border border-slate-100 p-8 shadow-lg max-w-none prose prose-slate">
|
|
1031
|
+
<p className="text-slate-600 leading-relaxed">
|
|
1032
|
+
Welcome to your new custom page template! You can design anything here in React, using standard HTML/JSX tags and Tailwind classes.
|
|
1033
|
+
</p>
|
|
1034
|
+
</div>
|
|
1035
|
+
</main>
|
|
1036
|
+
);
|
|
1037
|
+
}
|
|
1038
|
+
`;
|
|
1039
|
+
|
|
1040
|
+
writeFileSync(targetFile, pageTemplate, 'utf8');
|
|
1041
|
+
|
|
1042
|
+
console.log(pc.green(`\n⚡ Custom Page Template "${pascalCase}" successfully created!`));
|
|
1043
|
+
console.log(` Location: ${pc.cyan(`src/app/pages/${pascalCase}.tsx`)}`);
|
|
1044
|
+
console.log(` WordPress Template Name: ${pc.yellow(pascalCase)}`);
|
|
1045
|
+
console.log(`\n🎉 To link it in your application routing:`);
|
|
1046
|
+
console.log(` Add it inside your ${pc.cyan('src/app/routes.tsx')} file.\n`);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
993
1049
|
export function onSyncRoutes(themeRoot, { routesToScaffold, isForce, pc }) {
|
|
994
1050
|
function toPascalCase(str) {
|
|
995
1051
|
return str
|
|
@@ -1021,6 +1077,9 @@ export function onSyncRoutes(themeRoot, { routesToScaffold, isForce, pc }) {
|
|
|
1021
1077
|
*
|
|
1022
1078
|
* To force reset this page back to boilerplate defaults, run:
|
|
1023
1079
|
* 'pnpm forgewp sync:routes --force'
|
|
1080
|
+
*
|
|
1081
|
+
* This file compiles into a standalone WordPress Custom Page Template.
|
|
1082
|
+
* You can select it in the WordPress page editor dropdown as "Template: ${componentName}".
|
|
1024
1083
|
*/
|
|
1025
1084
|
import { WpHead } from "../../.forgewp/wordpress";
|
|
1026
1085
|
import { WpQueryLoop, useWpTitle, useWpExcerpt, useWpFeaturedImage } from "../../.forgewp/wordpress";
|
|
@@ -1154,7 +1213,7 @@ export function Single${pascalCpt}Page() {
|
|
|
1154
1213
|
// In production, forgeWpHydration.post.id holds the real WP numeric ID
|
|
1155
1214
|
const hydrationId =
|
|
1156
1215
|
typeof window !== 'undefined'
|
|
1157
|
-
?
|
|
1216
|
+
? window.forgeWpHydration?.post?.id || 0
|
|
1158
1217
|
: 0;
|
|
1159
1218
|
const id = hydrationId || routeParam;
|
|
1160
1219
|
|
|
@@ -1174,8 +1233,8 @@ export function Single${pascalCpt}Page() {
|
|
|
1174
1233
|
useWpContent() || devPost?.content || "<p>Loading details...</p>";
|
|
1175
1234
|
const rawImage = useWpFeaturedImage();
|
|
1176
1235
|
const hydrationImage =
|
|
1177
|
-
typeof window !== 'undefined' && !
|
|
1178
|
-
?
|
|
1236
|
+
typeof window !== 'undefined' && !window._forgeWpCompileTime
|
|
1237
|
+
? window.forgeWpHydration?.currentFeaturedImage || ""
|
|
1179
1238
|
: "";
|
|
1180
1239
|
const restImage =
|
|
1181
1240
|
typeof devPost?.featuredImage === "object" && devPost?.featuredImage !== null
|
package/lib/render-theme.mts
CHANGED
|
@@ -45,14 +45,12 @@ if (typeof globalThis.window === "undefined") {
|
|
|
45
45
|
querySelector: () => null,
|
|
46
46
|
querySelectorAll: () => [],
|
|
47
47
|
} as any;
|
|
48
|
+
globalThis.SVGElement = class SVGElement {} as any;
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
const { renderToStaticMarkup } = require("react-dom/server");
|
|
51
52
|
|
|
52
53
|
const appUrl = pathToFileURL(path.join(themeRoot, "src", "app", "page.tsx")).href;
|
|
53
|
-
const headerPath = path.join(themeRoot, "src", "components", "SiteHeader.tsx");
|
|
54
|
-
const headerUrl = pathToFileURL(headerPath).href;
|
|
55
|
-
const footerUrl = pathToFileURL(path.join(themeRoot, "src", "components", "SiteFooter.tsx")).href;
|
|
56
54
|
const layoutUrl = pathToFileURL(path.join(themeRoot, "src", "app", "layout.tsx")).href;
|
|
57
55
|
const layoutPath = path.join(themeRoot, "src", "app", "layout.tsx");
|
|
58
56
|
|
|
@@ -106,16 +104,30 @@ function renderPage(PageComponent: any): string {
|
|
|
106
104
|
}
|
|
107
105
|
|
|
108
106
|
let textDomain = "forgewp";
|
|
107
|
+
let themeConfig: any = {};
|
|
109
108
|
try {
|
|
110
109
|
const wpConfigPath = path.join(themeRoot, "wp.config.ts");
|
|
111
110
|
if (existsSync(wpConfigPath)) {
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
const wpConfigUrl = pathToFileURL(wpConfigPath).href;
|
|
112
|
+
const configModule = await import(wpConfigUrl);
|
|
113
|
+
themeConfig = configModule.default || configModule;
|
|
114
|
+
if (themeConfig.textDomain) {
|
|
115
|
+
textDomain = themeConfig.textDomain;
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
|
-
} catch (e) {
|
|
118
|
+
} catch (e) {
|
|
119
|
+
// fallback if import fails
|
|
120
|
+
try {
|
|
121
|
+
const wpConfigPath = path.join(themeRoot, "wp.config.ts");
|
|
122
|
+
if (existsSync(wpConfigPath)) {
|
|
123
|
+
const configContent = readFileSync(wpConfigPath, "utf8");
|
|
124
|
+
const domainMatch = configContent.match(/textDomain\s*:\s*['"]([^'"]+)['"]/);
|
|
125
|
+
if (domainMatch) {
|
|
126
|
+
textDomain = domainMatch[1];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} catch (e2) {}
|
|
130
|
+
}
|
|
119
131
|
|
|
120
132
|
function translateExpressionToPhp(expression: string, propName: string): string {
|
|
121
133
|
expression = expression.trim();
|
|
@@ -367,42 +379,97 @@ function buildHeadHtml(
|
|
|
367
379
|
}
|
|
368
380
|
|
|
369
381
|
// ── Render header / footer fragments ─────────────────────────────────────────
|
|
370
|
-
|
|
382
|
+
let headerFile = "";
|
|
383
|
+
let headerCompName = "";
|
|
384
|
+
|
|
385
|
+
if (themeConfig.headerPath) {
|
|
386
|
+
const absolutePath = path.isAbsolute(themeConfig.headerPath) ? themeConfig.headerPath : path.join(themeRoot, themeConfig.headerPath);
|
|
387
|
+
if (existsSync(absolutePath)) {
|
|
388
|
+
headerFile = absolutePath;
|
|
389
|
+
headerCompName = path.basename(absolutePath, path.extname(absolutePath));
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (!headerFile) {
|
|
394
|
+
const headerFallbacks = [
|
|
395
|
+
"src/components/SiteHeader.tsx",
|
|
396
|
+
"src/components/Header.tsx",
|
|
397
|
+
"src/components/Navbar.tsx",
|
|
398
|
+
"src/components/NavBar.tsx",
|
|
399
|
+
"src/components/Navigation.tsx"
|
|
400
|
+
];
|
|
401
|
+
for (const relPath of headerFallbacks) {
|
|
402
|
+
const p = path.join(themeRoot, relPath);
|
|
403
|
+
if (existsSync(p)) {
|
|
404
|
+
headerFile = p;
|
|
405
|
+
headerCompName = path.basename(p, path.extname(p));
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
371
410
|
|
|
372
411
|
let headerHtml = "";
|
|
373
|
-
if (
|
|
412
|
+
if (headerFile) {
|
|
374
413
|
try {
|
|
414
|
+
const headerUrl = pathToFileURL(headerFile).href;
|
|
375
415
|
const module = await import(headerUrl);
|
|
376
|
-
const
|
|
377
|
-
if (!
|
|
378
|
-
throw new Error(
|
|
416
|
+
const HeaderComponent = module[headerCompName] || module.default;
|
|
417
|
+
if (!HeaderComponent) {
|
|
418
|
+
throw new Error(`${headerCompName} component not found in export (expects named export '${headerCompName}' or default export).`);
|
|
379
419
|
}
|
|
380
|
-
headerHtml = renderToStaticMarkup(React.createElement(
|
|
420
|
+
headerHtml = renderToStaticMarkup(React.createElement(HeaderComponent));
|
|
381
421
|
} catch (err: any) {
|
|
382
|
-
console.error(`\n[ForgeWP Compiler Error] Failed to load or render the
|
|
422
|
+
console.error(`\n[ForgeWP Compiler Error] Failed to load or render the Header component (${headerFile}).`);
|
|
383
423
|
console.error(`Error details: ${err.stack || err.message || err}\n`);
|
|
384
424
|
process.exit(1);
|
|
385
425
|
}
|
|
386
426
|
} else {
|
|
387
|
-
console.warn("
|
|
427
|
+
console.warn("No separate Header component found, skipping separate header render");
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
let footerFile = "";
|
|
431
|
+
let footerCompName = "";
|
|
432
|
+
|
|
433
|
+
if (themeConfig.footerPath) {
|
|
434
|
+
const absolutePath = path.isAbsolute(themeConfig.footerPath) ? themeConfig.footerPath : path.join(themeRoot, themeConfig.footerPath);
|
|
435
|
+
if (existsSync(absolutePath)) {
|
|
436
|
+
footerFile = absolutePath;
|
|
437
|
+
footerCompName = path.basename(absolutePath, path.extname(absolutePath));
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (!footerFile) {
|
|
442
|
+
const footerFallbacks = [
|
|
443
|
+
"src/components/SiteFooter.tsx",
|
|
444
|
+
"src/components/Footer.tsx"
|
|
445
|
+
];
|
|
446
|
+
for (const relPath of footerFallbacks) {
|
|
447
|
+
const p = path.join(themeRoot, relPath);
|
|
448
|
+
if (existsSync(p)) {
|
|
449
|
+
footerFile = p;
|
|
450
|
+
footerCompName = path.basename(p, path.extname(p));
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
388
454
|
}
|
|
389
455
|
|
|
390
456
|
let footerHtml = "";
|
|
391
|
-
if (
|
|
457
|
+
if (footerFile) {
|
|
392
458
|
try {
|
|
459
|
+
const footerUrl = pathToFileURL(footerFile).href;
|
|
393
460
|
const module = await import(footerUrl);
|
|
394
|
-
const
|
|
395
|
-
if (!
|
|
396
|
-
throw new Error(
|
|
461
|
+
const FooterComponent = module[footerCompName] || module.default;
|
|
462
|
+
if (!FooterComponent) {
|
|
463
|
+
throw new Error(`${footerCompName} component not found in export (expects named export '${footerCompName}' or default export).`);
|
|
397
464
|
}
|
|
398
|
-
footerHtml = renderToStaticMarkup(React.createElement(
|
|
465
|
+
footerHtml = renderToStaticMarkup(React.createElement(FooterComponent));
|
|
399
466
|
} catch (err: any) {
|
|
400
|
-
console.error(`\n[ForgeWP Compiler Error] Failed to load or render the
|
|
467
|
+
console.error(`\n[ForgeWP Compiler Error] Failed to load or render the Footer component (${footerFile}).`);
|
|
401
468
|
console.error(`Error details: ${err.stack || err.message || err}\n`);
|
|
402
469
|
process.exit(1);
|
|
403
470
|
}
|
|
404
471
|
} else {
|
|
405
|
-
console.warn("
|
|
472
|
+
console.warn("No separate Footer component found, skipping separate footer render");
|
|
406
473
|
}
|
|
407
474
|
|
|
408
475
|
// ── Render pages ──────────────────────────────────────────────────────────────
|
|
@@ -522,7 +589,13 @@ if (existsSync(pagesDir)) {
|
|
|
522
589
|
const pageName = pageFile.replace(".tsx", "");
|
|
523
590
|
const slug = pageName.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
524
591
|
|
|
525
|
-
|
|
592
|
+
let finalHtml = cleanCustomHtml + stateScript;
|
|
593
|
+
if (module.pageConfig && typeof module.pageConfig === "object" && module.pageConfig.protected) {
|
|
594
|
+
const allowedAttr = module.pageConfig.allowed ? ` allowed="${module.pageConfig.allowed}"` : "";
|
|
595
|
+
const redirectAttr = module.pageConfig.redirect ? ` redirect="${module.pageConfig.redirect}"` : "";
|
|
596
|
+
finalHtml = `<forgewp-require-auth${allowedAttr}${redirectAttr}></forgewp-require-auth>` + finalHtml;
|
|
597
|
+
}
|
|
598
|
+
|
|
526
599
|
writeFileSync(path.join(outDir, `template-${slug}.html`), finalHtml, "utf8");
|
|
527
600
|
console.warn(`WROTE template-${slug}.html to ${outDir}`);
|
|
528
601
|
|
package/lib/validate-export.js
CHANGED
|
@@ -173,6 +173,55 @@ export async function validateExport({ themeRoot, outDir, assets, config, strict
|
|
|
173
173
|
if (!fs.existsSync(screenshotOut)) summary.missing.push(screenshotOut.replace(process.cwd() + path.sep, ''));
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
// WooCommerce Specific Audits
|
|
177
|
+
const packageJsonPath = path.join(themeRoot, 'package.json');
|
|
178
|
+
let isWooCommerce = false;
|
|
179
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
180
|
+
try {
|
|
181
|
+
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
182
|
+
isWooCommerce = !!(pkg.dependencies?.['@forgewp/woocommerce'] || pkg.devDependencies?.['@forgewp/woocommerce']);
|
|
183
|
+
} catch (e) {}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (isWooCommerce) {
|
|
187
|
+
if (fs.existsSync(outDir)) {
|
|
188
|
+
const phpFiles = fs.readdirSync(outDir)
|
|
189
|
+
.filter((file) => file.endsWith('.php'))
|
|
190
|
+
.map((file) => path.join(outDir, file));
|
|
191
|
+
|
|
192
|
+
for (const phpFile of phpFiles) {
|
|
193
|
+
const content = fs.readFileSync(phpFile, 'utf8');
|
|
194
|
+
|
|
195
|
+
// 1. XSS / Escaping Audits: Look for unescaped get_price_html()
|
|
196
|
+
if (content.includes('get_price_html()') && !content.includes('wp_kses_post')) {
|
|
197
|
+
summary.warnings.push(`${path.basename(phpFile)}: Found unescaped get_price_html(). Wrap with wp_kses_post() for XSS safety.`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Look for unescaped wc_get_rating_html()
|
|
201
|
+
if (content.includes('wc_get_rating_html') && !content.includes('wp_kses_post')) {
|
|
202
|
+
summary.warnings.push(`${path.basename(phpFile)}: Found unescaped wc_get_rating_html(). Wrap with wp_kses_post() for XSS safety.`);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 2. Loop optimization checks
|
|
206
|
+
if (content.includes("post_type' => 'product'") && !content.includes('wc_get_products')) {
|
|
207
|
+
summary.warnings.push(`${path.basename(phpFile)}: Catalog loop should use wc_get_products() pagination wrappers instead of raw post_type => product query.`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// 3. Hydrator Resource Hygiene check
|
|
213
|
+
const islands = scanForHydrationIslands(themeRoot);
|
|
214
|
+
if (islands.length === 0) {
|
|
215
|
+
const functionsPhpPath = path.join(outDir, 'functions.php');
|
|
216
|
+
if (fs.existsSync(functionsPhpPath)) {
|
|
217
|
+
const functionsPhp = fs.readFileSync(functionsPhpPath, 'utf8');
|
|
218
|
+
if (functionsPhp.includes('forgewp-hydrator.js') || functionsPhp.includes('forgewp-woocommerce')) {
|
|
219
|
+
summary.warnings.push('functions.php: Enqueuing forgewp-hydrator.js or registering woocommerce scripts even though no hydration islands were detected.');
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
176
225
|
// If strict, treat warnings as missing
|
|
177
226
|
if (strict && summary.warnings.length > 0) {
|
|
178
227
|
summary.missing.push(...summary.warnings);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forgewp/compiler",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Compile ForgeWP React themes into installable WordPress themes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"test:export-fixture": "node test/export-fixture/run-export-fixture.js",
|
|
42
|
+
"test:auth-transpilation": "node test/auth-transpilation.js",
|
|
42
43
|
"test:adapter-runtime": "node test/adapter-loader.js",
|
|
43
44
|
"test:adapter-types": "pnpm exec tsc --noEmit -p tsconfig.test.json"
|
|
44
45
|
}
|
|
@@ -43,7 +43,7 @@ export function PresetsStyle() {
|
|
|
43
43
|
const fontsHtml = googleFonts.length > 0
|
|
44
44
|
? `<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
45
45
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
46
|
-
<link href="https://fonts.googleapis.com/css2?family
|
|
46
|
+
<link href="https://fonts.googleapis.com/css2?family=${googleFonts.map((f: string) => encodeURIComponent(f)).join("&family=")}&display=swap" rel="stylesheet">`
|
|
47
47
|
: "";
|
|
48
48
|
|
|
49
49
|
const devCss = `
|