@forgewp/compiler 0.1.7 → 0.1.8
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 +29 -2
- package/bin/add.js +14 -2
- package/bin/clean.js +37 -0
- package/bin/cli.js +87 -0
- package/bin/doctor.js +99 -0
- package/bin/export.js +0 -0
- package/bin/make-block.js +130 -0
- package/bin/make-component.js +200 -0
- package/bin/make-post-type.js +129 -0
- package/bin/repair.js +62 -0
- package/lib/add-component.js +4 -1
- package/lib/blueprints.js +253 -72
- package/lib/generate-theme.js +187 -4
- package/lib/index.d.ts +1 -0
- package/package.json +3 -4
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
let rawName = args.find(a => !a.startsWith("-"));
|
|
9
|
+
|
|
10
|
+
// Support --name portfolio or --name=portfolio
|
|
11
|
+
const nameIndex = args.indexOf("--name");
|
|
12
|
+
if (nameIndex !== -1 && args[nameIndex + 1]) {
|
|
13
|
+
rawName = args[nameIndex + 1];
|
|
14
|
+
} else {
|
|
15
|
+
const nameEqual = args.find(a => a.startsWith("--name="));
|
|
16
|
+
if (nameEqual) {
|
|
17
|
+
rawName = nameEqual.split("=")[1];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!rawName) {
|
|
22
|
+
console.error(pc.red("\n❌ Error: Please specify a post type name."));
|
|
23
|
+
console.log(pc.cyan(" Example: pnpm forgewp make:post-type portfolio"));
|
|
24
|
+
console.log(pc.cyan(" Or: pnpm forgewp make:post-type --name portfolio\n"));
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// WordPress slugs should be lowercase alphanumeric with dashes or underscores
|
|
29
|
+
const cleanName = rawName.toLowerCase().replace(/[^a-z0-9_-]/g, "");
|
|
30
|
+
if (!cleanName) {
|
|
31
|
+
console.error(pc.red("\n❌ Error: Invalid post type name. Use letters, numbers, dashes, and underscores.\n"));
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const projectRoot = process.cwd();
|
|
36
|
+
const wpDir = path.join(projectRoot, "wordpress");
|
|
37
|
+
const mockDataPath = path.join(wpDir, "mock-data.json");
|
|
38
|
+
|
|
39
|
+
// Ensure wordpress directory exists
|
|
40
|
+
if (!existsSync(wpDir)) {
|
|
41
|
+
mkdirSync(wpDir, { recursive: true });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let mockData = {};
|
|
45
|
+
|
|
46
|
+
if (existsSync(mockDataPath)) {
|
|
47
|
+
try {
|
|
48
|
+
mockData = JSON.parse(readFileSync(mockDataPath, "utf8"));
|
|
49
|
+
} catch (err) {
|
|
50
|
+
console.error(pc.red(`\n❌ Error: Failed to parse wordpress/mock-data.json. Enforcing clean file.`));
|
|
51
|
+
mockData = {};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (mockData[cleanName]) {
|
|
56
|
+
console.warn(pc.yellow(`\n⚠️ Post type "${cleanName}" already exists in wordpress/mock-data.json.`));
|
|
57
|
+
console.log(` You can open the file directly to view or edit existing fields.\n`);
|
|
58
|
+
process.exit(0);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Parse custom fields if provided via --customFields, -customFields, --custom-fields, etc.
|
|
62
|
+
let customFieldsList = [];
|
|
63
|
+
const fieldsArg = args.find(a => /^-+custom[Ff]ields=/.test(a) || /^-+custom-fields=/.test(a));
|
|
64
|
+
if (fieldsArg) {
|
|
65
|
+
const rawFields = fieldsArg.split("=")[1];
|
|
66
|
+
if (rawFields) {
|
|
67
|
+
customFieldsList = rawFields.split(",").map(f => f.trim()).filter(Boolean);
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
const fieldsIndex = args.findIndex(a => /^-+custom[Ff]ields$/.test(a) || /^-+custom-fields$/.test(a));
|
|
71
|
+
if (fieldsIndex !== -1 && args[fieldsIndex + 1]) {
|
|
72
|
+
customFieldsList = args[fieldsIndex + 1].split(",").map(f => f.trim()).filter(Boolean);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Generate seeded customFields object
|
|
77
|
+
const seededCustomFields1 = {};
|
|
78
|
+
const seededCustomFields2 = {};
|
|
79
|
+
|
|
80
|
+
if (customFieldsList.length > 0) {
|
|
81
|
+
for (const field of customFieldsList) {
|
|
82
|
+
seededCustomFields1[field] = `[Seeded ${field} 1]`;
|
|
83
|
+
seededCustomFields2[field] = `[Seeded ${field} 2]`;
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
// Default fallbacks if no flag is provided
|
|
87
|
+
seededCustomFields1["client_name"] = "Mock Enterprise";
|
|
88
|
+
seededCustomFields1["project_budget"] = "$30,000";
|
|
89
|
+
|
|
90
|
+
seededCustomFields2["client_name"] = "Mock Organization";
|
|
91
|
+
seededCustomFields2["project_budget"] = "$55,000";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Generate template seed items for this custom post type
|
|
95
|
+
mockData[cleanName] = [
|
|
96
|
+
{
|
|
97
|
+
id: 1,
|
|
98
|
+
title: `Sample ${cleanName.charAt(0).toUpperCase() + cleanName.slice(1)} Item 1`,
|
|
99
|
+
excerpt: `This is a custom ${cleanName} post seeded dynamically via ForgeWP CLI.`,
|
|
100
|
+
content: `<p>Welcome to your new custom <strong>${cleanName}</strong> post template! Edit this in wordpress/mock-data.json.</p>`,
|
|
101
|
+
date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
|
|
102
|
+
author: "ForgeWP CLI",
|
|
103
|
+
featuredImage: `https://picsum.photos/seed/${cleanName}1/1200/630`,
|
|
104
|
+
customFields: seededCustomFields1
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
id: 2,
|
|
108
|
+
title: `Sample ${cleanName.charAt(0).toUpperCase() + cleanName.slice(1)} Item 2`,
|
|
109
|
+
excerpt: `This is another custom ${cleanName} post seeded dynamically via ForgeWP CLI.`,
|
|
110
|
+
content: `<p>This is the second custom post for the ${cleanName} post type.</p>`,
|
|
111
|
+
date: new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" }),
|
|
112
|
+
author: "ForgeWP CLI",
|
|
113
|
+
featuredImage: `https://picsum.photos/seed/${cleanName}2/1200/630`,
|
|
114
|
+
customFields: seededCustomFields2
|
|
115
|
+
}
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
// Write update JSON back to file
|
|
119
|
+
writeFileSync(mockDataPath, JSON.stringify(mockData, null, 2), "utf8");
|
|
120
|
+
|
|
121
|
+
const suggestedField = customFieldsList.length > 0 ? customFieldsList[0] : "client_name";
|
|
122
|
+
|
|
123
|
+
console.log(pc.green(`\n⚡ Post Type "${cleanName}" successfully registered!`));
|
|
124
|
+
console.log(` Local DB: ${pc.cyan(`wordpress/mock-data.json`)}`);
|
|
125
|
+
console.log(`\n🎉 Query your seeded fields in React inside:`);
|
|
126
|
+
console.log(` ${pc.yellow(`<WpQueryLoop postType="${cleanName}" postsPerPage={2}>`)}`);
|
|
127
|
+
console.log(` ${pc.yellow(`<h3>{useWpTitle()}</h3>`)}`);
|
|
128
|
+
console.log(` ${pc.yellow(`<p>${suggestedField.charAt(0).toUpperCase() + suggestedField.slice(1)}: {useWpCustomField("${suggestedField}")}</p>`)}`);
|
|
129
|
+
console.log(` ${pc.yellow(`</WpQueryLoop>`)}\n`);
|
package/bin/repair.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { existsSync, writeFileSync, mkdirSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
import { SYSTEM_BLUEPRINTS } from "../lib/blueprints.js";
|
|
7
|
+
|
|
8
|
+
console.log(`\n🔧 ${pc.bold(pc.bgYellow(pc.black(" FORGEWP SYSTEM REPAIR TOOL ")))}\n`);
|
|
9
|
+
|
|
10
|
+
const projectRoot = process.cwd();
|
|
11
|
+
let repairedCount = 0;
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
// Re-heal every system blueprint
|
|
15
|
+
for (const [relativePath, blueprintContent] of Object.entries(SYSTEM_BLUEPRINTS)) {
|
|
16
|
+
const fullPath = path.join(projectRoot, relativePath);
|
|
17
|
+
|
|
18
|
+
// Ensure the folder exists
|
|
19
|
+
const dirPath = path.dirname(fullPath);
|
|
20
|
+
if (!existsSync(dirPath)) {
|
|
21
|
+
mkdirSync(dirPath, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Force overwrite to restore pure internals
|
|
25
|
+
writeFileSync(fullPath, blueprintContent, "utf8");
|
|
26
|
+
console.log(` ${pc.green("✅ Restored/Healed")}: ${pc.cyan(relativePath)}`);
|
|
27
|
+
repairedCount++;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Ensure default mock data table is recreated if deleted
|
|
31
|
+
const mockDataPath = path.join(projectRoot, "wordpress", "mock-data.json");
|
|
32
|
+
if (!existsSync(mockDataPath)) {
|
|
33
|
+
const defaultJson = {
|
|
34
|
+
post: [
|
|
35
|
+
{
|
|
36
|
+
id: 1,
|
|
37
|
+
title: "Welcome to ForgeWP: The Headless Revolution",
|
|
38
|
+
excerpt: "Discover how ForgeWP bridges standard WordPress themes with React.",
|
|
39
|
+
content: "<p>Welcome to ForgeWP! Modify this in mock-data.json.</p>",
|
|
40
|
+
date: "May 10, 2026",
|
|
41
|
+
author: "Antigravity",
|
|
42
|
+
featuredImage: "https://picsum.photos/seed/forgewp/1200/630",
|
|
43
|
+
customFields: {}
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
};
|
|
47
|
+
const dirPath = path.dirname(mockDataPath);
|
|
48
|
+
if (!existsSync(dirPath)) {
|
|
49
|
+
mkdirSync(dirPath, { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
writeFileSync(mockDataPath, JSON.stringify(defaultJson, null, 2), "utf8");
|
|
52
|
+
console.log(` ${pc.green("✅ Seeded Default Database")}: wordpress/mock-data.json`);
|
|
53
|
+
repairedCount++;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
console.log("\n" + "─".repeat(60));
|
|
57
|
+
console.log(pc.green(`\n🎉 ${pc.bold("REPAIR COMPLETE:")} Successfully repaired/synced ${repairedCount} core framework elements.`));
|
|
58
|
+
console.log(pc.cyan(" All protected boundaries have been restored back to defaults. Build safe!\n"));
|
|
59
|
+
} catch (err) {
|
|
60
|
+
console.error(pc.red(`\n❌ Error: Failed to perform system repair.\nReason: ${err.message}\n`));
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
package/lib/add-component.js
CHANGED
|
@@ -20,7 +20,10 @@ export async function addComponent(component, options) {
|
|
|
20
20
|
registryPath = path.resolve(path.dirname(new URL(import.meta.url).pathname), "../../registry/components");
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
let localComponentFile = path.join(registryPath, component, `${style}.tsx`);
|
|
24
|
+
if (!existsSync(localComponentFile)) {
|
|
25
|
+
localComponentFile = path.join(registryPath, component, "forgewp.tsx");
|
|
26
|
+
}
|
|
24
27
|
|
|
25
28
|
if (existsSync(localComponentFile)) {
|
|
26
29
|
console.log(pc.dim(` Found ${component} in ForgeWP local registry...`));
|
package/lib/blueprints.js
CHANGED
|
@@ -7,65 +7,119 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
export const SYSTEM_BLUEPRINTS = {
|
|
10
|
+
"wordpress/mock-data.json": `{
|
|
11
|
+
"post": [
|
|
12
|
+
{
|
|
13
|
+
"id": 1,
|
|
14
|
+
"title": "Welcome to ForgeWP: The Headless Revolution",
|
|
15
|
+
"excerpt": "Discover how ForgeWP bridges standard WordPress themes with blistering fast React architectures.",
|
|
16
|
+
"content": "<p>This is the first mock post. In local dev, you can modify <code>wordpress/mock-data.json</code> to test layout content changes.</p>",
|
|
17
|
+
"date": "May 10, 2026",
|
|
18
|
+
"author": "Antigravity",
|
|
19
|
+
"featuredImage": "https://picsum.photos/seed/forgewp/1200/630",
|
|
20
|
+
"customFields": {}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"id": 2,
|
|
24
|
+
"title": "Unlocking Brutalist Web Design Aesthetics",
|
|
25
|
+
"excerpt": "A deep dive into high-contrast grids, sharp corners, and premium flat shadows in modern interfaces.",
|
|
26
|
+
"content": "<p>This is the second mock post. Style these grids using beautiful Tailwind utilities.</p>",
|
|
27
|
+
"date": "May 12, 2026",
|
|
28
|
+
"author": "DeepMind Partner",
|
|
29
|
+
"featuredImage": "https://picsum.photos/seed/brutalist/1200/630",
|
|
30
|
+
"customFields": {}
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
"project": [
|
|
34
|
+
{
|
|
35
|
+
"id": 1,
|
|
36
|
+
"title": "ForgeWP Scaffolding Platform",
|
|
37
|
+
"excerpt": "Building a custom compiler framework to transpile React elements into standard PHP themes.",
|
|
38
|
+
"content": "<p>Detailed description of the ForgeWP compiler pipeline project.</p>",
|
|
39
|
+
"date": "May 15, 2026",
|
|
40
|
+
"author": "Lead Architect",
|
|
41
|
+
"featuredImage": "https://picsum.photos/seed/platform/1200/630",
|
|
42
|
+
"customFields": {
|
|
43
|
+
"client_name": "El Dorado",
|
|
44
|
+
"project_budget": "$45,000"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"id": 2,
|
|
49
|
+
"title": "Brutalist Starter Theme",
|
|
50
|
+
"excerpt": "A clean, responsive, and robust high-contrast interface leveraging Tailwind CSS v4.",
|
|
51
|
+
"content": "<p>A brutalist theme showcasing raw black borders and vivid accent colors.</p>",
|
|
52
|
+
"date": "May 16, 2026",
|
|
53
|
+
"author": "Theme Engineer",
|
|
54
|
+
"featuredImage": "https://picsum.photos/seed/starter/1200/630",
|
|
55
|
+
"customFields": {
|
|
56
|
+
"client_name": "ForgeWP Community",
|
|
57
|
+
"project_budget": "Open Source"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
}`,
|
|
62
|
+
|
|
10
63
|
"src/lib/wordpress.tsx": `/**
|
|
11
64
|
* ForgeWP WordPress Data Hooks
|
|
12
65
|
*
|
|
13
66
|
* These hooks provide mock data during local development (Vite dev server).
|
|
14
67
|
* When the theme is exported, the ForgeWP compiler replaces every token
|
|
15
68
|
* with the equivalent WordPress PHP function call.
|
|
16
|
-
*
|
|
17
|
-
* ── Available hooks ────────────────────────────────────────────────────────
|
|
18
|
-
* useWpTitle() → the_title() / get_the_title()
|
|
19
|
-
* useWpContent() → the_content()
|
|
20
|
-
* useWpExcerpt() → the_excerpt()
|
|
21
|
-
* useWpPermalink() → get_permalink()
|
|
22
|
-
* useWpDate() → get_the_date()
|
|
23
|
-
* useWpAuthor() → get_the_author()
|
|
24
|
-
* useWpFeaturedImage() → get_the_post_thumbnail_url()
|
|
25
|
-
* useWpCategories() → the_category()
|
|
26
|
-
* useWpArchiveTitle() → the_archive_title()
|
|
27
|
-
* WpLoop → while ( have_posts() ) : the_post();
|
|
28
|
-
* ──────────────────────────────────────────────────────────────────────────
|
|
29
69
|
*/
|
|
30
|
-
import React from "react";
|
|
70
|
+
import React, { createContext, useContext } from "react";
|
|
71
|
+
|
|
72
|
+
// @ts-ignore
|
|
73
|
+
import mockData from "../../wordpress/mock-data.json";
|
|
31
74
|
|
|
32
75
|
const IS_DEV =
|
|
33
76
|
typeof import.meta !== "undefined" &&
|
|
34
77
|
// @ts-ignore
|
|
35
78
|
import.meta.env?.DEV === true;
|
|
36
79
|
|
|
80
|
+
// React context to support database-like query loops during local development
|
|
81
|
+
const WpPostContext = createContext<any>(null);
|
|
82
|
+
|
|
37
83
|
// ── Post content ─────────────────────────────────────────────────────────────
|
|
38
84
|
|
|
39
85
|
export function useWpTitle() {
|
|
40
|
-
if (IS_DEV)
|
|
86
|
+
if (IS_DEV) {
|
|
87
|
+
const post = useContext(WpPostContext);
|
|
88
|
+
return post?.title || "Sample WordPress Post Title";
|
|
89
|
+
}
|
|
41
90
|
return "__FORGEWP_THE_TITLE__";
|
|
42
91
|
}
|
|
43
92
|
|
|
44
93
|
export function useWpContent() {
|
|
45
94
|
if (IS_DEV) {
|
|
46
|
-
|
|
95
|
+
const post = useContext(WpPostContext);
|
|
96
|
+
return post?.content || "<p>This is sample post content rendered locally so you can design your theme. In WordPress, this is replaced by the actual content from the Gutenberg editor — including blocks, shortcodes, and embeds.</p>";
|
|
47
97
|
}
|
|
48
98
|
return "__FORGEWP_THE_CONTENT__";
|
|
49
99
|
}
|
|
50
100
|
|
|
51
101
|
export function useWpExcerpt() {
|
|
52
102
|
if (IS_DEV) {
|
|
53
|
-
|
|
103
|
+
const post = useContext(WpPostContext);
|
|
104
|
+
return post?.excerpt || "A short excerpt that gives readers a quick preview of what to expect in the full post.";
|
|
54
105
|
}
|
|
55
106
|
return "__FORGEWP_THE_EXCERPT__";
|
|
56
107
|
}
|
|
57
108
|
|
|
58
109
|
export function useWpPermalink() {
|
|
59
|
-
if (IS_DEV)
|
|
110
|
+
if (IS_DEV) {
|
|
111
|
+
const post = useContext(WpPostContext);
|
|
112
|
+
return post ? \`/post/\${post.id}\` : "/post";
|
|
113
|
+
}
|
|
60
114
|
return "__FORGEWP_THE_PERMALINK__";
|
|
61
115
|
}
|
|
62
116
|
|
|
63
117
|
// ── Post meta ─────────────────────────────────────────────────────────────────
|
|
64
118
|
|
|
65
|
-
/** Returns the formatted post date. Maps to get_the_date() in WordPress. */
|
|
66
119
|
export function useWpDate() {
|
|
67
120
|
if (IS_DEV) {
|
|
68
|
-
|
|
121
|
+
const post = useContext(WpPostContext);
|
|
122
|
+
return post?.date || new Date().toLocaleDateString("en-US", {
|
|
69
123
|
year: "numeric",
|
|
70
124
|
month: "long",
|
|
71
125
|
day: "numeric",
|
|
@@ -74,33 +128,22 @@ export function useWpDate() {
|
|
|
74
128
|
return "__FORGEWP_THE_DATE__";
|
|
75
129
|
}
|
|
76
130
|
|
|
77
|
-
/** Returns the post author's display name. Maps to get_the_author() in WordPress. */
|
|
78
131
|
export function useWpAuthor() {
|
|
79
|
-
if (IS_DEV)
|
|
132
|
+
if (IS_DEV) {
|
|
133
|
+
const post = useContext(WpPostContext);
|
|
134
|
+
return post?.author || "Jane Doe";
|
|
135
|
+
}
|
|
80
136
|
return "__FORGEWP_THE_AUTHOR__";
|
|
81
137
|
}
|
|
82
138
|
|
|
83
|
-
/**
|
|
84
|
-
* Returns the featured image URL (large size).
|
|
85
|
-
* Maps to get_the_post_thumbnail_url(null, 'large') in WordPress.
|
|
86
|
-
*
|
|
87
|
-
* In dev, returns a placeholder image from picsum.photos.
|
|
88
|
-
* In your JSX, use it as: <img src={featuredImage} alt={title} />
|
|
89
|
-
*/
|
|
90
139
|
export function useWpFeaturedImage() {
|
|
91
|
-
if (IS_DEV)
|
|
140
|
+
if (IS_DEV) {
|
|
141
|
+
const post = useContext(WpPostContext);
|
|
142
|
+
return post?.featuredImage || "https://picsum.photos/seed/forgewp/1200/630";
|
|
143
|
+
}
|
|
92
144
|
return "__FORGEWP_THE_POST_THUMBNAIL_URL__";
|
|
93
145
|
}
|
|
94
146
|
|
|
95
|
-
/**
|
|
96
|
-
* Returns the post's categories as an HTML string (links separated by commas).
|
|
97
|
-
* Maps to the_category(', ') in WordPress.
|
|
98
|
-
*
|
|
99
|
-
* Use with dangerouslySetInnerHTML since it contains anchor tags.
|
|
100
|
-
* @example
|
|
101
|
-
* const categories = useWpCategories();
|
|
102
|
-
* <div dangerouslySetInnerHTML={{ __html: categories }} />
|
|
103
|
-
*/
|
|
104
147
|
export function useWpCategories() {
|
|
105
148
|
if (IS_DEV) {
|
|
106
149
|
return '<a href="#">Technology</a>, <a href="#">Design</a>';
|
|
@@ -108,11 +151,6 @@ export function useWpCategories() {
|
|
|
108
151
|
return "__FORGEWP_THE_CATEGORY_LIST__";
|
|
109
152
|
}
|
|
110
153
|
|
|
111
|
-
/**
|
|
112
|
-
* Returns the archive page title (e.g. "Category: Technology", "Tag: React").
|
|
113
|
-
* Maps to the_archive_title() in WordPress.
|
|
114
|
-
* Use this in archive.tsx — not needed for single posts.
|
|
115
|
-
*/
|
|
116
154
|
export function useWpArchiveTitle() {
|
|
117
155
|
if (IS_DEV) return "Category: Technology";
|
|
118
156
|
return "__FORGEWP_THE_ARCHIVE_TITLE__";
|
|
@@ -120,33 +158,28 @@ export function useWpArchiveTitle() {
|
|
|
120
158
|
|
|
121
159
|
// ── Loop ──────────────────────────────────────────────────────────────────────
|
|
122
160
|
|
|
123
|
-
/**
|
|
124
|
-
* \`<WpLoop>\` — Renders a WordPress post loop.
|
|
125
|
-
*
|
|
126
|
-
* In dev: renders your children 3 times with mock data so you can design your layout.
|
|
127
|
-
* On export: wraps your JSX in a real \`while (have_posts()) : the_post();\` PHP loop.
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* <WpLoop>
|
|
131
|
-
* <PostCard />
|
|
132
|
-
* </WpLoop>
|
|
133
|
-
*/
|
|
134
161
|
export function WpLoop({ children }: { children: React.ReactNode }) {
|
|
135
162
|
if (IS_DEV) {
|
|
136
|
-
|
|
163
|
+
const posts = mockData?.post || [
|
|
164
|
+
{ id: 1, title: "Mock Post 1" },
|
|
165
|
+
{ id: 2, title: "Mock Post 2" },
|
|
166
|
+
{ id: 3, title: "Mock Post 3" }
|
|
167
|
+
];
|
|
168
|
+
|
|
137
169
|
return (
|
|
138
170
|
<>
|
|
139
|
-
{
|
|
140
|
-
|
|
141
|
-
|
|
171
|
+
{posts.map((post: any) => (
|
|
172
|
+
<WpPostContext.Provider key={post.id} value={post}>
|
|
173
|
+
{children}
|
|
174
|
+
</WpPostContext.Provider>
|
|
175
|
+
))}
|
|
142
176
|
</>
|
|
143
177
|
);
|
|
144
178
|
}
|
|
145
179
|
|
|
146
|
-
// In production: custom elements become compiler tokens → PHP loop
|
|
147
180
|
return (
|
|
148
181
|
<>
|
|
149
|
-
{/* @ts-ignore
|
|
182
|
+
{/* @ts-ignore */}
|
|
150
183
|
<forgewp-loop-start />
|
|
151
184
|
{children}
|
|
152
185
|
{/* @ts-ignore */}
|
|
@@ -154,6 +187,129 @@ export function WpLoop({ children }: { children: React.ReactNode }) {
|
|
|
154
187
|
</>
|
|
155
188
|
);
|
|
156
189
|
}
|
|
190
|
+
|
|
191
|
+
// ── Custom Field Mapping (ACF / Meta Fields) ──────────────────────────────────
|
|
192
|
+
|
|
193
|
+
export function useWpCustomField(fieldName: string, defaultValue: string = ""): string {
|
|
194
|
+
if (IS_DEV) {
|
|
195
|
+
const post = useContext(WpPostContext);
|
|
196
|
+
if (post && post.customFields && typeof post.customFields[fieldName] !== "undefined") {
|
|
197
|
+
return post.customFields[fieldName];
|
|
198
|
+
}
|
|
199
|
+
return defaultValue || "[Mock custom field: " + fieldName + "]";
|
|
200
|
+
}
|
|
201
|
+
return "__FORGEWP_CUSTOM_FIELD__" + fieldName + "__";
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ── Navigation Menu Component ──────────────────────────────────────────────────
|
|
205
|
+
|
|
206
|
+
export interface WpMenuProps {
|
|
207
|
+
location?: string;
|
|
208
|
+
className?: string;
|
|
209
|
+
linkClassName?: string;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function WpMenu({ location = "primary", className = "", linkClassName = "" }: WpMenuProps) {
|
|
213
|
+
if (IS_DEV) {
|
|
214
|
+
const mockItems = [
|
|
215
|
+
{ title: "Home", url: "/" },
|
|
216
|
+
{ title: "Blog", url: "/post" },
|
|
217
|
+
{ title: "Archive", url: "/archive" },
|
|
218
|
+
];
|
|
219
|
+
return (
|
|
220
|
+
<nav className={className}>
|
|
221
|
+
{mockItems.map((item, idx) => (
|
|
222
|
+
<a key={idx} href={item.url} className={linkClassName}>
|
|
223
|
+
{item.title}
|
|
224
|
+
</a>
|
|
225
|
+
))}
|
|
226
|
+
</nav>
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return (
|
|
231
|
+
// @ts-ignore
|
|
232
|
+
<forgewp-menu location={location} className={className} linkClassName={linkClassName} />
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ── Custom Query Loop Component ───────────────────────────────────────────────
|
|
237
|
+
|
|
238
|
+
export interface WpQueryLoopProps {
|
|
239
|
+
postType?: string;
|
|
240
|
+
postsPerPage?: number;
|
|
241
|
+
categoryName?: string;
|
|
242
|
+
children: React.ReactNode;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function WpQueryLoop({
|
|
246
|
+
postType = "post",
|
|
247
|
+
postsPerPage = 3,
|
|
248
|
+
categoryName = "",
|
|
249
|
+
children
|
|
250
|
+
}: WpQueryLoopProps) {
|
|
251
|
+
if (IS_DEV) {
|
|
252
|
+
const allPosts = (mockData as any)?.[postType] || [];
|
|
253
|
+
const posts = allPosts.slice(0, postsPerPage);
|
|
254
|
+
|
|
255
|
+
if (posts.length === 0) {
|
|
256
|
+
const fallbacks = Array.from({ length: postsPerPage }).map((_, i) => ({
|
|
257
|
+
id: i + 1,
|
|
258
|
+
title: \`Mock \${postType} \${i + 1}\`,
|
|
259
|
+
}));
|
|
260
|
+
return (
|
|
261
|
+
<>
|
|
262
|
+
{fallbacks.map((post: any) => (
|
|
263
|
+
<WpPostContext.Provider key={post.id} value={post}>
|
|
264
|
+
{children}
|
|
265
|
+
</WpPostContext.Provider>
|
|
266
|
+
))}
|
|
267
|
+
</>
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<>
|
|
273
|
+
{posts.map((post: any) => (
|
|
274
|
+
<WpPostContext.Provider key={post.id} value={post}>
|
|
275
|
+
{children}
|
|
276
|
+
</WpPostContext.Provider>
|
|
277
|
+
))}
|
|
278
|
+
</>
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return (
|
|
283
|
+
<>
|
|
284
|
+
{/* @ts-ignore */}
|
|
285
|
+
<forgewp-query-loop-start postType={postType} postsPerPage={postsPerPage} categoryName={categoryName} />
|
|
286
|
+
{children}
|
|
287
|
+
{/* @ts-ignore */}
|
|
288
|
+
<forgewp-query-loop-end />
|
|
289
|
+
</>
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// ── Shortcodes Component ────────────────────────────────────────────────────────
|
|
294
|
+
|
|
295
|
+
export interface WpShortcodeProps {
|
|
296
|
+
code: string;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function WpShortcode({ code }: WpShortcodeProps) {
|
|
300
|
+
if (IS_DEV) {
|
|
301
|
+
return (
|
|
302
|
+
<div className="p-4 bg-zinc-100 border-2 border-dashed border-zinc-400 font-mono text-xs text-zinc-600 rounded-none my-4">
|
|
303
|
+
<span className="font-bold text-zinc-800">WordPress Shortcode Preview:</span> {code}
|
|
304
|
+
</div>
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return (
|
|
309
|
+
// @ts-ignore
|
|
310
|
+
<forgewp-shortcode code={code} />
|
|
311
|
+
);
|
|
312
|
+
}
|
|
157
313
|
`,
|
|
158
314
|
|
|
159
315
|
"src/lib/SEO.tsx": `import * as ReactHelmetAsync from "react-helmet-async";
|
|
@@ -275,11 +431,27 @@ const IS_DEV =
|
|
|
275
431
|
*
|
|
276
432
|
* Dynamically injects WordPress-preset CSS Custom Properties and enqueues Google Fonts
|
|
277
433
|
* during local Vite development, keeping your styles perfectly in sync with WordPress.
|
|
278
|
-
* In production builds, this returns
|
|
279
|
-
*
|
|
434
|
+
* In production builds, this returns only the registry-aesthetic design tokens (like --radius)
|
|
435
|
+
* to keep shadcn components and theme variables responsive to your config.
|
|
280
436
|
*/
|
|
281
437
|
export function PresetsStyle() {
|
|
282
|
-
|
|
438
|
+
const themeStyle = wpConfig.style || "forgewp";
|
|
439
|
+
|
|
440
|
+
// Production build: only inject custom registry-aesthetic design properties
|
|
441
|
+
if (!IS_DEV) {
|
|
442
|
+
const prodCss = \`
|
|
443
|
+
:root {
|
|
444
|
+
--radius: \${themeStyle === "forgewp" ? "0px" : "0.5rem"};
|
|
445
|
+
--radius-sm: calc(var(--radius) - 4px);
|
|
446
|
+
--radius-md: calc(var(--radius) - 2px);
|
|
447
|
+
--radius-lg: var(--radius);
|
|
448
|
+
--border-width: \${themeStyle === "forgewp" ? "2px" : "1px"};
|
|
449
|
+
--border-color: \${themeStyle === "forgewp" ? "#09090b" : "#e4e4e7"};
|
|
450
|
+
--shadow-offset: \${themeStyle === "forgewp" ? "4px" : "0px"};
|
|
451
|
+
}
|
|
452
|
+
\`;
|
|
453
|
+
return <style dangerouslySetInnerHTML={{ __html: prodCss }} />;
|
|
454
|
+
}
|
|
283
455
|
|
|
284
456
|
// Local development fallbacks for WordPress CSS presets
|
|
285
457
|
const colors = wpConfig.settings?.color?.palette || [];
|
|
@@ -292,23 +464,32 @@ export function PresetsStyle() {
|
|
|
292
464
|
const fontsHtml = googleFonts.length > 0
|
|
293
465
|
? \`<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
294
466
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
295
|
-
<link href="https://fonts.googleapis.com/css2?family
|
|
467
|
+
<link href="https://fonts.googleapis.com/css2?family=\&{googleFonts.map(f => encodeURIComponent(f)).join("&family=")}&display=swap" rel="stylesheet">\`
|
|
296
468
|
: "";
|
|
297
469
|
|
|
298
|
-
const
|
|
470
|
+
const devCss = \`
|
|
299
471
|
:root {
|
|
300
|
-
\${colors.map((c) =>
|
|
301
|
-
\${fontSizes.map((f) =>
|
|
302
|
-
\${fontFamilies.map((f) =>
|
|
303
|
-
\${layout.contentSize ?
|
|
304
|
-
\${layout.wideSize ?
|
|
472
|
+
\${colors.map((c) => "--wp--preset--color--" + c.slug + ": " + c.color + ";").join("\\n ")}
|
|
473
|
+
\${fontSizes.map((f) => "--wp--preset--font-size--" + f.slug + ": " + f.size + ";").join("\\n ")}
|
|
474
|
+
\${fontFamilies.map((f) => "--wp--preset--font-family--" + f.slug + ": " + f.fontFamily + ";").join("\\n ")}
|
|
475
|
+
\${layout.contentSize ? "--wp--style--global--content-size: " + layout.contentSize + ";" : ""}
|
|
476
|
+
\${layout.wideSize ? "--wp--style--global--wide-size: " + layout.wideSize + ";" : ""}
|
|
477
|
+
|
|
478
|
+
/* Registry Aesthetic Mode Custom Properties */
|
|
479
|
+
--radius: \${themeStyle === "forgewp" ? "0px" : "0.5rem"};
|
|
480
|
+
--radius-sm: calc(var(--radius) - 4px);
|
|
481
|
+
--radius-md: calc(var(--radius) - 2px);
|
|
482
|
+
--radius-lg: var(--radius);
|
|
483
|
+
--border-width: \${themeStyle === "forgewp" ? "2px" : "1px"};
|
|
484
|
+
--border-color: \${themeStyle === "forgewp" ? "#09090b" : "#e4e4e7"};
|
|
485
|
+
--shadow-offset: \${themeStyle === "forgewp" ? "4px" : "0px"};
|
|
305
486
|
}
|
|
306
487
|
\`;
|
|
307
488
|
|
|
308
489
|
return (
|
|
309
490
|
<>
|
|
310
491
|
{fontsHtml && <span dangerouslySetInnerHTML={{ __html: fontsHtml }} />}
|
|
311
|
-
<style dangerouslySetInnerHTML={{ __html:
|
|
492
|
+
<style dangerouslySetInnerHTML={{ __html: devCss }} />
|
|
312
493
|
</>
|
|
313
494
|
);
|
|
314
495
|
}
|