@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.
- package/README.md +59 -0
- package/bin/add.js +20 -14
- package/bin/cli.js +145 -70
- package/bin/doctor.js +21 -12
- package/bin/export.js +69 -20
- package/bin/fresh.js +5 -0
- package/bin/i18n-extract.js +3 -10
- package/bin/i18n-translate.js +15 -24
- package/bin/make-block.js +13 -0
- package/bin/make-editable.js +166 -0
- package/bin/make-form.js +131 -0
- package/bin/make-loop.js +40 -43
- package/bin/make-mail.js +27 -0
- package/bin/make-post-type.js +57 -29
- package/bin/make-sandbox.js +62 -11
- package/bin/make-section.js +221 -0
- package/bin/make-shell.js +375 -0
- package/bin/repair.js +14 -26
- package/bin/seed.js +119 -0
- package/bin/sync-routes.js +107 -85
- package/lib/adapters/html-adapter.js +5 -0
- package/lib/adapters/stub-adapter.js +4 -0
- package/lib/add-component.js +153 -92
- package/lib/asset-loader.js +313 -3
- package/lib/assets/asset-graph.js +432 -0
- package/lib/assets/asset-pipeline.js +125 -0
- package/lib/assets/diagnostics-report.js +345 -0
- package/lib/assets/external-fetcher.js +173 -0
- package/lib/assets/font-orchestrator.js +348 -0
- package/lib/assets/font-usage-scanner.js +242 -0
- package/lib/assets/image-optimizer.js +229 -0
- package/lib/assets/index.js +51 -0
- package/lib/assets/preload-generator.js +70 -0
- package/lib/assets/public-url.js +153 -0
- package/lib/block-authoring-help.js +11 -0
- package/lib/blocks/ast-parser.js +67 -0
- package/lib/blocks/discover-blocks.js +146 -0
- package/lib/blocks/editor-scope.js +733 -0
- package/lib/blocks/editor-transpiler.js +1229 -0
- package/lib/blocks/index.js +2838 -0
- package/lib/blocks/php-transpiler.js +4928 -0
- package/lib/blocks/scanner.js +1487 -0
- package/lib/blocks/shared-utils.js +290 -0
- package/lib/blocks/source-sanitize.js +1449 -0
- package/lib/blueprints.js +31 -91
- package/lib/build-assets.js +38 -33
- package/lib/collect-page-template-routes.js +102 -0
- package/lib/collect-static-routes.js +175 -0
- package/lib/diagnostics.js +58 -33
- package/lib/export-theme.js +84 -5
- package/lib/extract-template-content.js +90 -0
- package/lib/forms-preview.js +42 -0
- package/lib/functions/cpt-routing.js +69 -0
- package/lib/functions/forms.js +2461 -0
- package/lib/functions/hydration-enqueuer.js +400 -0
- package/lib/functions/index.js +1623 -0
- package/lib/functions/load-menus.js +112 -0
- package/lib/functions/load-translations.js +148 -0
- package/lib/functions/rest-endpoints.js +2923 -0
- package/lib/functions/routing.js +765 -0
- package/lib/functions/seed-mock-data.js +392 -0
- package/lib/functions/seed-products.js +481 -0
- package/lib/functions/settings-page.js +273 -0
- package/lib/functions/theme-setup.js +494 -0
- package/lib/generate-theme.js +1916 -339
- package/lib/hydration/editable-schemas.js +332 -0
- package/lib/hydration/enter-hidden-initial.js +65 -0
- package/lib/hydration/form-schemas.js +227 -0
- package/lib/hydration/index.js +12 -0
- package/lib/hydration/is-interactive.js +951 -0
- package/lib/hydration/island-split.js +1231 -0
- package/lib/hydration/islands-scanner.js +599 -0
- package/lib/hydration/resolve-island-chunk.js +103 -0
- package/lib/hydration/resolve-island-component.js +52 -0
- package/lib/hydration/rollup-inputs.js +81 -0
- package/lib/hydration/root-class-extractor.js +220 -0
- package/lib/hydration/scan-app-providers.js +154 -0
- package/lib/hydration/static-lint.js +382 -0
- package/lib/hydration/transform.js +257 -0
- package/lib/hydration/uses-block-area.js +49 -0
- package/lib/hydration/utils.js +119 -0
- package/lib/i18n/translation-cache.js +141 -0
- package/lib/icon-registry.js +72 -0
- package/lib/index.d.ts +287 -8
- package/lib/index.js +2 -1
- package/lib/load-config.js +338 -242
- package/lib/mail-templates.js +37 -0
- package/lib/markup-processor.js +388 -102
- package/lib/node-runtime.js +7 -0
- package/lib/page-config-plugin.js +64 -20
- package/lib/php-builders.js +383 -44
- package/lib/react-adapter.js +983 -235
- package/lib/registration-metadata.js +11 -0
- package/lib/render-theme.mts +1410 -616
- package/lib/route-layout.js +76 -0
- package/lib/template-discovery.js +58 -0
- package/lib/theme-compatibility.js +32 -0
- package/lib/validate-export.js +2 -2
- package/lib/validate.js +27 -5
- package/lib/virtual-plugin.js +319 -0
- package/lib/walkthrough.md +26 -0
- package/package.json +16 -5
- package/templates/auth/cms/email-logs.json +9 -0
- package/templates/auth/cms/menus.json +11 -0
- package/templates/auth/cms/mock-data.json +123 -0
- package/templates/auth/cms/roles.json +7 -0
- package/templates/auth/cms/sessions.json +1 -0
- package/templates/auth/cms/site-settings.json +28 -0
- package/templates/auth/cms/translations.json +4 -0
- package/templates/auth/cms/users.json +46 -0
- package/templates/auth/components/forgot-password-form-wrapper.tsx +10 -2
- package/templates/auth/components/login-form-wrapper.tsx +118 -21
- package/templates/auth/components/reset-password-form-wrapper.tsx +10 -2
- package/templates/auth/components/signup-form-wrapper.tsx +2 -2
- package/templates/auth/components/verify-email-view-wrapper.tsx +5 -5
- package/templates/auth/pages/DashboardPage.tsx +1 -4
- package/templates/auth/pages/ForgotPasswordPage.tsx +1 -4
- package/templates/auth/pages/LoginPage.tsx +1 -4
- package/templates/auth/pages/ResetPasswordPage.tsx +1 -4
- package/templates/auth/pages/SignUpPage.tsx +1 -4
- package/templates/auth/pages/VerifyEmailPage.tsx +1 -4
- package/templates/auth/ui/button.tsx +57 -0
- package/templates/auth/ui/card.tsx +76 -0
- package/templates/auth/ui/field.tsx +242 -0
- package/templates/auth/ui/input.tsx +22 -0
- package/templates/auth/ui/label.tsx +24 -0
- package/templates/auth/ui/separator.tsx +31 -0
- package/templates/menus.json +12 -10
- package/templates/users.json +8 -4
- package/templates/wordpress.tsx +690 -245
- package/lib/block-compiler.js +0 -552
- package/lib/functions-builder.js +0 -2970
- package/lib/hydration-scanner.js +0 -833
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
import { parseSource, traverse } from '../blocks/ast-parser.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Converts a Babel AST literal/object/array node to a JS value.
|
|
8
|
+
*/
|
|
9
|
+
function astToValue(node) {
|
|
10
|
+
if (!node) return null;
|
|
11
|
+
switch (node.type) {
|
|
12
|
+
case 'StringLiteral':
|
|
13
|
+
return node.value;
|
|
14
|
+
case 'NumericLiteral':
|
|
15
|
+
return node.value;
|
|
16
|
+
case 'BooleanLiteral':
|
|
17
|
+
return node.value;
|
|
18
|
+
case 'NullLiteral':
|
|
19
|
+
return null;
|
|
20
|
+
case 'ArrayExpression':
|
|
21
|
+
return node.elements.map((el) => astToValue(el));
|
|
22
|
+
case 'ObjectExpression': {
|
|
23
|
+
const obj = {};
|
|
24
|
+
for (const prop of node.properties) {
|
|
25
|
+
if (prop.type === 'ObjectProperty') {
|
|
26
|
+
const key = prop.key.name || prop.key.value;
|
|
27
|
+
obj[key] = astToValue(prop.value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return obj;
|
|
31
|
+
}
|
|
32
|
+
case 'UnaryExpression': {
|
|
33
|
+
if (node.operator === '-' && node.argument?.type === 'NumericLiteral') {
|
|
34
|
+
return -node.argument.value;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
case 'CallExpression': {
|
|
39
|
+
// Handles defineProducts([...]) wrapper
|
|
40
|
+
if (node.arguments && node.arguments[0]) {
|
|
41
|
+
return astToValue(node.arguments[0]);
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
default:
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Loads products from cms/products.ts or cms/products.json.
|
|
52
|
+
* @param {string} themeRoot
|
|
53
|
+
* @returns {Array<any>}
|
|
54
|
+
*/
|
|
55
|
+
export function loadProductsData(themeRoot) {
|
|
56
|
+
const tsPath = path.join(themeRoot, 'cms', 'products.ts');
|
|
57
|
+
const jsonPath = path.join(themeRoot, 'cms', 'products.json');
|
|
58
|
+
|
|
59
|
+
if (fs.existsSync(tsPath)) {
|
|
60
|
+
try {
|
|
61
|
+
const code = fs.readFileSync(tsPath, 'utf8');
|
|
62
|
+
const ast = parseSource(code);
|
|
63
|
+
let extracted = null;
|
|
64
|
+
|
|
65
|
+
traverse(ast, {
|
|
66
|
+
ExportNamedDeclaration(astPath) {
|
|
67
|
+
const decl = astPath.node.declaration;
|
|
68
|
+
if (decl && decl.declarations) {
|
|
69
|
+
for (const d of decl.declarations) {
|
|
70
|
+
if (d.id?.name === 'products' && d.init) {
|
|
71
|
+
extracted = astToValue(d.init);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
ExportDefaultDeclaration(astPath) {
|
|
77
|
+
if (!extracted && astPath.node.declaration) {
|
|
78
|
+
extracted = astToValue(astPath.node.declaration);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
if (Array.isArray(extracted)) return extracted;
|
|
84
|
+
if (extracted && Array.isArray(extracted.products)) return extracted.products;
|
|
85
|
+
} catch (e) {
|
|
86
|
+
console.warn(`⚠️ [ForgeWP Seed] Could not load ${tsPath}: ${e.message}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (fs.existsSync(jsonPath)) {
|
|
91
|
+
try {
|
|
92
|
+
const raw = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
|
|
93
|
+
if (Array.isArray(raw)) return raw;
|
|
94
|
+
if (raw && Array.isArray(raw.products)) return raw.products;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
console.warn(`⚠️ [ForgeWP Seed] Could not parse ${jsonPath}: ${e.message}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Builds the PHP code for seeding WooCommerce products.
|
|
105
|
+
* @param {import('../types.js').ForgeWPThemeConfig} config
|
|
106
|
+
* @param {string} themeRoot
|
|
107
|
+
* @returns {string}
|
|
108
|
+
*/
|
|
109
|
+
export function buildSeedProductsPhp(config, themeRoot) {
|
|
110
|
+
const seedConfig = config.seed;
|
|
111
|
+
if (!seedConfig || !seedConfig.products) {
|
|
112
|
+
return '';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const strategy = seedConfig.products === true ? 'once' : seedConfig.products;
|
|
116
|
+
const developmentOnly = seedConfig.developmentOnly !== false;
|
|
117
|
+
const sideloadImages = seedConfig.sideloadImages !== false;
|
|
118
|
+
|
|
119
|
+
const rawProducts = loadProductsData(themeRoot);
|
|
120
|
+
if (!rawProducts || rawProducts.length === 0) {
|
|
121
|
+
return '';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Normalize product objects for PHP export
|
|
125
|
+
const normalizedProducts = rawProducts.map((p, index) => {
|
|
126
|
+
const slug = p.slug || `product-${p.id || index + 1}`;
|
|
127
|
+
const name = p.name || p.title || `Product ${index + 1}`;
|
|
128
|
+
const price = typeof p.price === 'number' ? p.price : parseFloat(p.price || '0');
|
|
129
|
+
const salePrice = p.salePrice !== undefined ? (typeof p.salePrice === 'number' ? p.salePrice : parseFloat(p.salePrice || '0')) : null;
|
|
130
|
+
const regularPrice = p.regular_price !== undefined ? (typeof p.regular_price === 'number' ? p.regular_price : parseFloat(p.regular_price || '0')) : price;
|
|
131
|
+
const description = p.description || p.content || '';
|
|
132
|
+
const shortDescription = p.shortDescription || p.excerpt || '';
|
|
133
|
+
const sku = p.sku || `FWP-${slug.toUpperCase()}`;
|
|
134
|
+
const images = Array.isArray(p.images) ? p.images : (p.featuredImage ? [p.featuredImage] : []);
|
|
135
|
+
|
|
136
|
+
// Categories
|
|
137
|
+
let categories = [];
|
|
138
|
+
if (Array.isArray(p.categories)) {
|
|
139
|
+
categories = p.categories;
|
|
140
|
+
} else if (p.category) {
|
|
141
|
+
categories = [p.category];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Tags
|
|
145
|
+
let tags = [];
|
|
146
|
+
if (Array.isArray(p.tags)) {
|
|
147
|
+
tags = p.tags;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Dimensions & Weight
|
|
151
|
+
const dimensions = p.dimensions || {};
|
|
152
|
+
const weight = p.weight !== undefined ? String(p.weight) : '';
|
|
153
|
+
|
|
154
|
+
const reviews = Array.isArray(p.reviews)
|
|
155
|
+
? p.reviews.map((r, rIdx) => ({
|
|
156
|
+
id: r.id || rIdx + 1,
|
|
157
|
+
author: r.author || 'Customer',
|
|
158
|
+
email: r.email || `customer${rIdx + 1}@example.com`,
|
|
159
|
+
content: r.content || r.comment || '',
|
|
160
|
+
rating: typeof r.rating === 'number' ? r.rating : 5,
|
|
161
|
+
date: r.date || new Date().toISOString().split('T')[0],
|
|
162
|
+
verified: r.verified !== false,
|
|
163
|
+
}))
|
|
164
|
+
: [];
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
sku,
|
|
168
|
+
name,
|
|
169
|
+
slug,
|
|
170
|
+
regular_price: regularPrice > 0 ? String(regularPrice) : '',
|
|
171
|
+
sale_price: salePrice && salePrice > 0 ? String(salePrice) : '',
|
|
172
|
+
description,
|
|
173
|
+
short_description: shortDescription,
|
|
174
|
+
categories,
|
|
175
|
+
tags,
|
|
176
|
+
images,
|
|
177
|
+
weight,
|
|
178
|
+
length: dimensions.length !== undefined ? String(dimensions.length) : '',
|
|
179
|
+
width: dimensions.width !== undefined ? String(dimensions.width) : '',
|
|
180
|
+
height: dimensions.height !== undefined ? String(dimensions.height) : '',
|
|
181
|
+
badge: p.badge || '',
|
|
182
|
+
materials: Array.isArray(p.materials) ? p.materials : [],
|
|
183
|
+
care: p.care || '',
|
|
184
|
+
rating: p.rating || p.average_rating || null,
|
|
185
|
+
rating_count: p.reviewCount || p.rating_count || null,
|
|
186
|
+
reviews,
|
|
187
|
+
};
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const productsJson = JSON.stringify(normalizedProducts, null, 2);
|
|
191
|
+
const dataHash = crypto.createHash('sha256').update(productsJson).digest('hex');
|
|
192
|
+
|
|
193
|
+
return `
|
|
194
|
+
/**
|
|
195
|
+
* ==============================================================================
|
|
196
|
+
* ForgeWP WooCommerce Products Data Seeder
|
|
197
|
+
* Strategy: ${strategy} | Hash: ${dataHash.substring(0, 12)}
|
|
198
|
+
* ==============================================================================
|
|
199
|
+
*/
|
|
200
|
+
function forgewp_seed_woocommerce_products($force = false) {
|
|
201
|
+
if (!class_exists('WooCommerce')) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
${developmentOnly ? `
|
|
205
|
+
// Production Safety Guard: only run in WP_DEBUG mode unless forced
|
|
206
|
+
if (!$force && (!defined('WP_DEBUG') || !WP_DEBUG)) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
` : ''}
|
|
210
|
+
$current_hash = '${dataHash}';
|
|
211
|
+
$saved_hash = get_option('forgewp_seeded_products_hash', '');
|
|
212
|
+
$strategy = '${strategy}';
|
|
213
|
+
|
|
214
|
+
// In 'once' mode, skip if already seeded or if any products already exist
|
|
215
|
+
if (!$force && $strategy === 'once') {
|
|
216
|
+
if (!empty($saved_hash)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
$existing_count = wp_count_posts('product');
|
|
220
|
+
if (!empty($existing_count->publish) && intval($existing_count->publish) > 0) {
|
|
221
|
+
update_option('forgewp_seeded_products_hash', $current_hash);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// In 'upsert' mode, skip if content hash matches
|
|
227
|
+
if (!$force && $strategy === 'upsert' && $saved_hash === $current_hash) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
$raw_json = <<<'FORGEWP_PRODUCTS_JSON'
|
|
232
|
+
${productsJson}
|
|
233
|
+
FORGEWP_PRODUCTS_JSON;
|
|
234
|
+
|
|
235
|
+
$products = json_decode($raw_json, true);
|
|
236
|
+
if (!is_array($products)) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Ensure media handling helpers exist if sideloading images
|
|
241
|
+
if (!function_exists('media_sideload_image')) {
|
|
242
|
+
require_once ABSPATH . 'wp-admin/includes/media.php';
|
|
243
|
+
require_once ABSPATH . 'wp-admin/includes/file.php';
|
|
244
|
+
require_once ABSPATH . 'wp-admin/includes/image.php';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
foreach ($products as $p) {
|
|
248
|
+
$sku = !empty($p['sku']) ? sanitize_text_field($p['sku']) : '';
|
|
249
|
+
$slug = !empty($p['slug']) ? sanitize_title($p['slug']) : '';
|
|
250
|
+
$name = !empty($p['name']) ? sanitize_text_field($p['name']) : '';
|
|
251
|
+
|
|
252
|
+
if (empty($name)) {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Find existing product by SKU or slug
|
|
257
|
+
$product_id = 0;
|
|
258
|
+
if (!empty($sku)) {
|
|
259
|
+
$product_id = wc_get_product_id_by_sku($sku);
|
|
260
|
+
}
|
|
261
|
+
if (!$product_id && !empty($slug)) {
|
|
262
|
+
$existing_post = get_page_by_path($slug, OBJECT, 'product');
|
|
263
|
+
if ($existing_post) {
|
|
264
|
+
$product_id = $existing_post->ID;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if ($product_id && $strategy === 'once' && !$force) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
$wc_product = $product_id ? wc_get_product($product_id) : new WC_Product_Simple();
|
|
273
|
+
if (!$wc_product) {
|
|
274
|
+
$wc_product = new WC_Product_Simple();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
$wc_product->set_name($name);
|
|
278
|
+
if (!empty($slug)) {
|
|
279
|
+
$wc_product->set_slug($slug);
|
|
280
|
+
}
|
|
281
|
+
if (!empty($sku)) {
|
|
282
|
+
$wc_product->set_sku($sku);
|
|
283
|
+
}
|
|
284
|
+
$wc_product->set_status('publish');
|
|
285
|
+
$wc_product->set_catalog_visibility('visible');
|
|
286
|
+
|
|
287
|
+
// Pricing
|
|
288
|
+
if (!empty($p['regular_price'])) {
|
|
289
|
+
$wc_product->set_regular_price($p['regular_price']);
|
|
290
|
+
$wc_product->set_price($p['regular_price']);
|
|
291
|
+
}
|
|
292
|
+
if (!empty($p['sale_price'])) {
|
|
293
|
+
$wc_product->set_sale_price($p['sale_price']);
|
|
294
|
+
$wc_product->set_price($p['sale_price']);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Descriptions
|
|
298
|
+
if (!empty($p['description'])) {
|
|
299
|
+
$wc_product->set_description($p['description']);
|
|
300
|
+
}
|
|
301
|
+
if (!empty($p['short_description'])) {
|
|
302
|
+
$wc_product->set_short_description($p['short_description']);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Dimensions & Specs
|
|
306
|
+
if (!empty($p['weight'])) {
|
|
307
|
+
$wc_product->set_weight($p['weight']);
|
|
308
|
+
}
|
|
309
|
+
if (!empty($p['length'])) {
|
|
310
|
+
$wc_product->set_length($p['length']);
|
|
311
|
+
}
|
|
312
|
+
if (!empty($p['width'])) {
|
|
313
|
+
$wc_product->set_width($p['width']);
|
|
314
|
+
}
|
|
315
|
+
if (!empty($p['height'])) {
|
|
316
|
+
$wc_product->set_height($p['height']);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Categories (product_cat)
|
|
320
|
+
if (!empty($p['categories']) && is_array($p['categories'])) {
|
|
321
|
+
$cat_ids = array();
|
|
322
|
+
foreach ($p['categories'] as $cat_name) {
|
|
323
|
+
$term = get_term_by('name', $cat_name, 'product_cat');
|
|
324
|
+
if (!$term) {
|
|
325
|
+
$new_term = wp_insert_term($cat_name, 'product_cat');
|
|
326
|
+
if (!is_wp_error($new_term) && isset($new_term['term_id'])) {
|
|
327
|
+
$cat_ids[] = intval($new_term['term_id']);
|
|
328
|
+
}
|
|
329
|
+
} else {
|
|
330
|
+
$cat_ids[] = intval($term->term_id);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (!empty($cat_ids)) {
|
|
334
|
+
$wc_product->set_category_ids($cat_ids);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Tags (product_tag)
|
|
339
|
+
if (!empty($p['tags']) && is_array($p['tags'])) {
|
|
340
|
+
$tag_ids = array();
|
|
341
|
+
foreach ($p['tags'] as $tag_name) {
|
|
342
|
+
$term = get_term_by('name', $tag_name, 'product_tag');
|
|
343
|
+
if (!$term) {
|
|
344
|
+
$new_term = wp_insert_term($tag_name, 'product_tag');
|
|
345
|
+
if (!is_wp_error($new_term) && isset($new_term['term_id'])) {
|
|
346
|
+
$tag_ids[] = intval($new_term['term_id']);
|
|
347
|
+
}
|
|
348
|
+
} else {
|
|
349
|
+
$tag_ids[] = intval($term->term_id);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (!empty($tag_ids)) {
|
|
353
|
+
$wc_product->set_tag_ids($tag_ids);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Stock status
|
|
358
|
+
$wc_product->set_stock_status('instock');
|
|
359
|
+
|
|
360
|
+
$saved_id = $wc_product->save();
|
|
361
|
+
|
|
362
|
+
if ($saved_id && !is_wp_error($saved_id)) {
|
|
363
|
+
// Save custom metadata
|
|
364
|
+
if (!empty($p['badge'])) {
|
|
365
|
+
update_post_meta($saved_id, '_forgewp_badge', sanitize_text_field($p['badge']));
|
|
366
|
+
}
|
|
367
|
+
if (!empty($p['materials'])) {
|
|
368
|
+
update_post_meta($saved_id, '_forgewp_materials', $p['materials']);
|
|
369
|
+
}
|
|
370
|
+
if (!empty($p['care'])) {
|
|
371
|
+
update_post_meta($saved_id, '_forgewp_care', sanitize_text_field($p['care']));
|
|
372
|
+
}
|
|
373
|
+
if (!empty($p['images']) && is_array($p['images'])) {
|
|
374
|
+
update_post_meta($saved_id, '_forgewp_images', $p['images']);
|
|
375
|
+
if (!empty($p['images'][0])) {
|
|
376
|
+
update_post_meta($saved_id, '_forgewp_image', esc_url_raw($p['images'][0]));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
// Seeding actual WooCommerce reviews into wp_comments
|
|
380
|
+
if (!empty($p['reviews']) && is_array($p['reviews'])) {
|
|
381
|
+
$existing_reviews = get_comments(array(
|
|
382
|
+
'post_id' => $saved_id,
|
|
383
|
+
'type' => 'review',
|
|
384
|
+
'count' => true,
|
|
385
|
+
));
|
|
386
|
+
|
|
387
|
+
if (intval($existing_reviews) === 0) {
|
|
388
|
+
$total_rating = 0;
|
|
389
|
+
$rating_counts = array();
|
|
390
|
+
foreach ($p['reviews'] as $rev) {
|
|
391
|
+
$rev_rating = isset($rev['rating']) ? intval($rev['rating']) : 5;
|
|
392
|
+
$rev_author = !empty($rev['author']) ? sanitize_text_field($rev['author']) : 'Customer';
|
|
393
|
+
$rev_email = !empty($rev['email']) ? sanitize_email($rev['email']) : 'customer@example.com';
|
|
394
|
+
$rev_content = !empty($rev['content']) ? sanitize_textarea_field($rev['content']) : '';
|
|
395
|
+
$rev_date = !empty($rev['date']) ? $rev['date'] : current_time('mysql');
|
|
396
|
+
|
|
397
|
+
$comment_id = wp_insert_comment(array(
|
|
398
|
+
'comment_post_ID' => $saved_id,
|
|
399
|
+
'comment_author' => $rev_author,
|
|
400
|
+
'comment_author_email' => $rev_email,
|
|
401
|
+
'comment_content' => $rev_content,
|
|
402
|
+
'comment_type' => 'review',
|
|
403
|
+
'comment_approved' => 1,
|
|
404
|
+
'comment_date' => $rev_date,
|
|
405
|
+
));
|
|
406
|
+
|
|
407
|
+
if ($comment_id && !is_wp_error($comment_id)) {
|
|
408
|
+
update_comment_meta($comment_id, 'rating', $rev_rating);
|
|
409
|
+
if (!empty($rev['verified'])) {
|
|
410
|
+
update_comment_meta($comment_id, 'verified', 1);
|
|
411
|
+
}
|
|
412
|
+
$total_rating += $rev_rating;
|
|
413
|
+
$rating_counts[$rev_rating] = isset($rating_counts[$rev_rating]) ? $rating_counts[$rev_rating] + 1 : 1;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
$rev_count = count($p['reviews']);
|
|
418
|
+
$avg_rating = $rev_count > 0 ? round($total_rating / $rev_count, 2) : 0;
|
|
419
|
+
update_post_meta($saved_id, '_wc_review_count', $rev_count);
|
|
420
|
+
update_post_meta($saved_id, '_wc_rating_count', $rating_counts);
|
|
421
|
+
update_post_meta($saved_id, '_wc_average_rating', $avg_rating);
|
|
422
|
+
}
|
|
423
|
+
} else {
|
|
424
|
+
$existing_reviews = get_comments(array(
|
|
425
|
+
'post_id' => $saved_id,
|
|
426
|
+
'type' => 'review',
|
|
427
|
+
'count' => true,
|
|
428
|
+
));
|
|
429
|
+
if (intval($existing_reviews) === 0) {
|
|
430
|
+
update_post_meta($saved_id, '_wc_review_count', 0);
|
|
431
|
+
update_post_meta($saved_id, '_wc_rating_count', array());
|
|
432
|
+
update_post_meta($saved_id, '_wc_average_rating', 0);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Image Sideloading (if active and not already attached)
|
|
437
|
+
${sideloadImages ? `
|
|
438
|
+
if (!empty($p['images']) && is_array($p['images']) && !has_post_thumbnail($saved_id)) {
|
|
439
|
+
$gallery_ids = array();
|
|
440
|
+
foreach ($p['images'] as $img_idx => $img_url) {
|
|
441
|
+
if (filter_var($img_url, FILTER_VALIDATE_URL)) {
|
|
442
|
+
$attach_id = media_sideload_image($img_url, $saved_id, $name, 'id');
|
|
443
|
+
if (!is_wp_error($attach_id) && is_numeric($attach_id)) {
|
|
444
|
+
if ($img_idx === 0) {
|
|
445
|
+
set_post_thumbnail($saved_id, intval($attach_id));
|
|
446
|
+
} else {
|
|
447
|
+
$gallery_ids[] = intval($attach_id);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
if (!empty($gallery_ids)) {
|
|
453
|
+
update_post_meta($saved_id, '_product_image_gallery', implode(',', $gallery_ids));
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
` : ''}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
update_option('forgewp_seeded_products_hash', $current_hash);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Hook into theme activation and admin initialization
|
|
464
|
+
add_action('after_switch_theme', 'forgewp_seed_woocommerce_products');
|
|
465
|
+
add_action('admin_init', function() {
|
|
466
|
+
if (current_user_can('manage_woocommerce')) {
|
|
467
|
+
forgewp_seed_woocommerce_products();
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
// WP-CLI command registration: wp forgewp seed-products
|
|
472
|
+
if (defined('WP_CLI') && WP_CLI) {
|
|
473
|
+
WP_CLI::add_command('forgewp seed-products', function($args, $assoc_args) {
|
|
474
|
+
$force = isset($assoc_args['force']);
|
|
475
|
+
WP_CLI::log('🌱 Seeding WooCommerce products from ForgeWP CMS catalog...');
|
|
476
|
+
forgewp_seed_woocommerce_products($force);
|
|
477
|
+
WP_CLI::success('WooCommerce products successfully synchronized!');
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
`;
|
|
481
|
+
}
|