@forgewp/compiler 0.3.0 → 0.4.2
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 +24 -5
- package/bin/analyze.js +158 -0
- package/bin/cli.js +33 -4
- package/bin/doctor.js +125 -11
- package/bin/export.js +20 -0
- package/bin/fresh.js +32 -343
- package/bin/i18n-extract.js +121 -0
- package/bin/i18n-translate.js +112 -0
- package/bin/make-block.js +27 -60
- package/bin/make-island.js +72 -0
- package/bin/make-post-type.js +6 -6
- package/bin/make-template.js +145 -0
- package/bin/repair.js +2 -2
- package/bin/sync-hooks.js +221 -0
- package/bin/sync-routes.js +28 -158
- package/define-config.d.ts +1 -0
- package/define-config.js +1 -0
- package/lib/adapters/html-adapter.js +65 -0
- package/lib/adapters/stub-adapter.js +49 -0
- package/lib/add-component.js +282 -183
- package/lib/asset-loader.js +27 -0
- package/lib/block-compiler.js +552 -0
- package/lib/blueprints.js +15 -501
- package/lib/define-config.d.ts +3 -0
- package/lib/define-config.js +8 -0
- package/lib/diagnostics.js +798 -0
- package/lib/export-theme.js +81 -18
- package/lib/framework-adapter.js +34 -0
- package/lib/functions-builder.js +2044 -0
- package/lib/generate-theme.js +1140 -1352
- package/lib/hydration-scanner.js +663 -21
- package/lib/i18n/extractor.js +60 -0
- package/lib/i18n/providers.js +165 -0
- package/lib/index.d.ts +115 -3
- package/lib/index.js +19 -5
- package/lib/load-config.js +122 -6
- package/lib/markup-processor.js +589 -0
- package/lib/php-builders.js +381 -0
- package/lib/react-adapter.js +1336 -0
- package/lib/render-static.js +9 -82
- package/lib/render-theme.mts +543 -232
- package/lib/screenshot-fallback.png +0 -0
- package/lib/types.js +1 -0
- package/lib/validate-export.js +183 -0
- package/package.json +15 -3
- package/templates/PresetsStyle.tsx +74 -0
- package/templates/SEO.tsx +109 -0
- package/templates/forgewp-config.ts +43 -0
- package/templates/menus.json +11 -0
- package/templates/mock-data.json +434 -0
- package/templates/wordpress.tsx +1297 -0
- package/bin/make-component.js +0 -200
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process markup: fix nav links, replace ForgeWP dynamic template tags with PHP equivalents, etc.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} html Raw HTML markup
|
|
5
|
+
* @param {string} textDomain Text domain for translations
|
|
6
|
+
* @returns {string} Transpiled HTML containing PHP tags
|
|
7
|
+
*/
|
|
8
|
+
export function processMarkup(html, textDomain = 'theme') {
|
|
9
|
+
if (!html) return '';
|
|
10
|
+
// Fix nav links — # → <?php echo esc_url( home_url( '/' ) ); ?>
|
|
11
|
+
// We use a placeholder and replace it in the PHP file generation if needed,
|
|
12
|
+
// but for now we'll do it via string replacement in the template files.
|
|
13
|
+
let processed = html.replace(
|
|
14
|
+
/href="#"/g,
|
|
15
|
+
'href="<?php echo esc_url( home_url( \'/\' ) ); ?>"',
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
// Map React root link (href="/") to dynamic localized WordPress home URL
|
|
19
|
+
processed = processed.replace(
|
|
20
|
+
/href=(["'])\/\1/g,
|
|
21
|
+
'href="<?php echo esc_url( function_exists( \'pll_home_url\' ) ? pll_home_url( function_exists( \'pll_current_language\' ) ? pll_current_language() : \'\' ) : home_url( \'/\' ) ); ?>"',
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
processed = processed.replace(
|
|
25
|
+
/__FORGEWP_THE_TITLE__/g,
|
|
26
|
+
'<?php echo html_entity_decode( get_the_title(), ENT_QUOTES | ENT_HTML5, \'UTF-8\' ); ?>',
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
processed = processed.replace(
|
|
30
|
+
/__FORGEWP_THE_CONTENT__/g,
|
|
31
|
+
'<?php the_content(); ?>',
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
processed = processed.replace(
|
|
35
|
+
/__FORGEWP_THE_PERMALINK__/g,
|
|
36
|
+
'<?php the_permalink(); ?>',
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
processed = processed.replace(
|
|
40
|
+
/__FORGEWP_THE_EXCERPT__/g,
|
|
41
|
+
'<?php the_excerpt(); ?>',
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
processed = processed.replace(
|
|
45
|
+
/__FORGEWP_THE_DATE__/g,
|
|
46
|
+
'<?php echo esc_html( get_the_date() ); ?>',
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
processed = processed.replace(
|
|
50
|
+
/__FORGEWP_THE_MODIFIED_DATE__/g,
|
|
51
|
+
'<?php echo esc_html( get_the_modified_date() ); ?>',
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
processed = processed.replace(
|
|
55
|
+
/__FORGEWP_THE_AUTHOR__/g,
|
|
56
|
+
'<?php echo esc_html( get_the_author() ); ?>',
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
processed = processed.replace(
|
|
60
|
+
/__FORGEWP_THE_POST_THUMBNAIL_URL__/g,
|
|
61
|
+
"<?php echo esc_url( get_the_post_thumbnail_url( null, 'large' ) ); ?>",
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
processed = processed.replace(
|
|
65
|
+
/__FORGEWP_THE_CATEGORY_LIST__/g,
|
|
66
|
+
"<?php the_category( ', ' ); ?>",
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
processed = processed.replace(
|
|
70
|
+
/__FORGEWP_TAXONOMY_LIST_([a-zA-Z0-9_-]+)__/g,
|
|
71
|
+
"<?php echo html_entity_decode( wp_strip_all_tags( get_the_term_list( get_the_ID(), '$1', '', ', ' ) ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); ?>",
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
processed = processed.replace(
|
|
75
|
+
/__FORGEWP_THE_ARCHIVE_TITLE__/g,
|
|
76
|
+
'<?php the_archive_title(); ?>',
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
processed = processed.replace(
|
|
80
|
+
/<forgewp-loop-start\s*\/?>/g,
|
|
81
|
+
'<?php if (have_posts()) : while (have_posts()) : the_post(); ?>',
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
processed = processed.replace(/<\/forgewp-loop-start>/g, '');
|
|
85
|
+
|
|
86
|
+
processed = processed.replace(
|
|
87
|
+
/<forgewp-loop-end\s*\/?>/g,
|
|
88
|
+
'<?php endwhile; else : echo "<p>No posts found.</p>"; endif; ?>',
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
processed = processed.replace(/<\/forgewp-loop-end>/g, '');
|
|
92
|
+
|
|
93
|
+
// ── Custom Meta Fields (ACF / metadata support) ──
|
|
94
|
+
processed = processed.replace(
|
|
95
|
+
/__FORGEWP_CUSTOM_FIELD__([a-zA-Z0-9_-]+)__/g,
|
|
96
|
+
"<?php echo esc_html( get_post_meta( get_the_ID(), '$1', true ) ); ?>",
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
processed = processed.replace(
|
|
100
|
+
/__FORGEWP_META_([a-zA-Z0-9_-]+)_DEFAULT_(.*?)__/g,
|
|
101
|
+
(match, key, defaultValEncoded) => {
|
|
102
|
+
const decoded = decodeURIComponent(defaultValEncoded);
|
|
103
|
+
const escapedDecoded = decoded.replace(/'/g, "\\'");
|
|
104
|
+
const cleanDecoded = escapedDecoded.replace(
|
|
105
|
+
/__FORGEWP_I18N_([^_](?:[^_]|_(?!_))*?)__/g,
|
|
106
|
+
(m, text) => {
|
|
107
|
+
const cleanText = text
|
|
108
|
+
.replace(/'/g, "'")
|
|
109
|
+
.replace(/"/g, '"')
|
|
110
|
+
.replace(/&/g, '&')
|
|
111
|
+
.replace(/'/g, "\\'");
|
|
112
|
+
return `' . __('${cleanText}', '${textDomain}') . '`;
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
const isRichText = decoded.includes('<p>') || decoded.includes('</p>') || decoded.includes('<br') || key.includes('subtitle') || key.includes('content') || key.includes('bio');
|
|
116
|
+
return `<?php echo forgewp_get_meta_value( '${key}', '${cleanDecoded}', ${isRichText ? 'true' : 'false'} ); ?>`;
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
// ── Dynamic Custom Menus ──
|
|
121
|
+
processed = processed.replace(
|
|
122
|
+
/<forgewp-menu\s+([^>]*)\/?>/g,
|
|
123
|
+
(match, attrsStr) => {
|
|
124
|
+
const getAttr = (name) => {
|
|
125
|
+
const regex = new RegExp(
|
|
126
|
+
`(?:${name}|${name.toLowerCase()})=(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`,
|
|
127
|
+
'i',
|
|
128
|
+
);
|
|
129
|
+
const m = attrsStr.match(regex);
|
|
130
|
+
return m ? m[1] || m[2] || m[3] || '' : '';
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const location = getAttr('location') || 'primary';
|
|
134
|
+
const className = getAttr('class') || getAttr('className') || '';
|
|
135
|
+
const linkClassName =
|
|
136
|
+
getAttr('linkClassName') || getAttr('linkclassname') || '';
|
|
137
|
+
|
|
138
|
+
return `<?php
|
|
139
|
+
$lang = function_exists('pll_current_language') ? pll_current_language() : (defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : '');
|
|
140
|
+
$menu_id = function_exists('forgewp_get_menu_id_for_lang') ? forgewp_get_menu_id_for_lang('${location}', $lang) : null;
|
|
141
|
+
$menu_items = $menu_id ? wp_get_nav_menu_items($menu_id) : array();
|
|
142
|
+
if (!empty($menu_items)) {
|
|
143
|
+
echo '<nav class="${className}">';
|
|
144
|
+
foreach ($menu_items as $item) {
|
|
145
|
+
echo '<a href="' . esc_url($item->url) . '" class="${linkClassName}">' . esc_html($item->title) . '</a>';
|
|
146
|
+
}
|
|
147
|
+
echo '</nav>';
|
|
148
|
+
} else {
|
|
149
|
+
echo '<nav class="${className}"><a href="' . esc_url(home_url('/')) . '" class="${linkClassName}">Home</a></nav>';
|
|
150
|
+
}
|
|
151
|
+
?>`;
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
processed = processed.replace(/<\/forgewp-menu>/g, '');
|
|
155
|
+
|
|
156
|
+
// ── WpLanguageSwitcher → pll_the_languages() ──
|
|
157
|
+
processed = processed.replace(
|
|
158
|
+
/<forgewp-language-switcher\s+([^>]*)\/?\>/g,
|
|
159
|
+
(match, attrsStr) => {
|
|
160
|
+
const getAttr = (name) => {
|
|
161
|
+
const regex = new RegExp(
|
|
162
|
+
`(?:${name}|${name.toLowerCase()})=(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`,
|
|
163
|
+
'i',
|
|
164
|
+
);
|
|
165
|
+
const m = attrsStr.match(regex);
|
|
166
|
+
return m ? m[1] || m[2] || m[3] || '' : '';
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const className = getAttr('class') || getAttr('className') || '';
|
|
170
|
+
const linkClass = getAttr('linkClassName') || getAttr('linkclassname') || '';
|
|
171
|
+
const showFlags = getAttr('showFlags') === '1';
|
|
172
|
+
const showNames = getAttr('showNames') === '1';
|
|
173
|
+
const showCodes = getAttr('showCodes') !== '0'; // default true
|
|
174
|
+
|
|
175
|
+
const flagsPart = showFlags ? "echo '<img src=\"' . esc_url($l['flag']) . '\" alt=\"' . esc_attr($l['name']) . '\" />';" : '';
|
|
176
|
+
const codesPart = showCodes ? "echo esc_html(strtoupper($l['slug']));" : '';
|
|
177
|
+
const namesPart = showNames ? "echo ' ' . esc_html($l['name']);" : '';
|
|
178
|
+
|
|
179
|
+
return `<?php
|
|
180
|
+
if (function_exists('pll_the_languages')) {
|
|
181
|
+
$pll_langs = pll_the_languages(array('raw' => 1));
|
|
182
|
+
if (is_array($pll_langs)) {
|
|
183
|
+
echo '<nav class="${className}">';
|
|
184
|
+
foreach ($pll_langs as $pll_l) {
|
|
185
|
+
$pll_is_current = !empty($pll_l['current_lang']);
|
|
186
|
+
$pll_link_class = trim('${linkClass}' . ($pll_is_current ? ' active' : ''));
|
|
187
|
+
echo '<a href="' . esc_url($pll_l['url']) . '" class="' . esc_attr($pll_link_class) . '" hreflang="' . esc_attr($pll_l['slug']) . '" lang="' . esc_attr($pll_l['slug']) . '">';
|
|
188
|
+
${flagsPart}
|
|
189
|
+
${codesPart}
|
|
190
|
+
${namesPart}
|
|
191
|
+
echo '</a>';
|
|
192
|
+
}
|
|
193
|
+
echo '</nav>';
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
// Fallback: plain home link when no multilingual plugin is active
|
|
197
|
+
echo '<nav class="${className}"><a href="' . esc_url(home_url('/')) . '" class="${linkClass}">${showCodes ? 'DE' : ''}</a></nav>';
|
|
198
|
+
} ?>`;
|
|
199
|
+
},
|
|
200
|
+
);
|
|
201
|
+
processed = processed.replace(/<\/forgewp-language-switcher>/g, '');
|
|
202
|
+
|
|
203
|
+
// ── useWpI18n strings → esc_html__() ──
|
|
204
|
+
// Matches __FORGEWP_I18N_<text>__ and emits <?php echo __('text', 'textdomain'); ?>
|
|
205
|
+
// The regex allows spaces and most characters inside the token; double-underscore terminates.
|
|
206
|
+
processed = processed.replace(
|
|
207
|
+
/__FORGEWP_I18N_([^_](?:[^_]|_(?!_))*?)__/g,
|
|
208
|
+
(match, text) => {
|
|
209
|
+
// Decode any HTML entities React may have encoded
|
|
210
|
+
const clean = text
|
|
211
|
+
.replace(/'/g, "'")
|
|
212
|
+
.replace(/"/g, '"')
|
|
213
|
+
.replace(/&/g, '&')
|
|
214
|
+
.replace(/'/g, "\\'");
|
|
215
|
+
return `<?php echo __('${clean}', '${textDomain}'); ?>`;
|
|
216
|
+
},
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
// ── WordPress Shortcodes Support ──
|
|
220
|
+
processed = processed.replace(
|
|
221
|
+
/<forgewp-shortcode\s+[^>]*code="([^"]+)"\s*\/?>/g,
|
|
222
|
+
(match, code) => {
|
|
223
|
+
const decodedCode = code
|
|
224
|
+
.replace(/"/g, '"')
|
|
225
|
+
.replace(/'/g, "'")
|
|
226
|
+
.replace(/'/g, "'")
|
|
227
|
+
.replace(/&/g, '&');
|
|
228
|
+
const escapedCode = decodedCode.replace(/'/g, "\\'");
|
|
229
|
+
return `<?php echo do_shortcode('${escapedCode}'); ?>`;
|
|
230
|
+
},
|
|
231
|
+
);
|
|
232
|
+
processed = processed.replace(/<\/forgewp-shortcode>/g, '');
|
|
233
|
+
|
|
234
|
+
// ── WordPress Bounded Block Areas Support ──
|
|
235
|
+
processed = processed.replace(
|
|
236
|
+
/<forgewp-block-area\s+[^>]*name="([^"]+)"\s*\/?>/g,
|
|
237
|
+
'<?php the_content(); ?>',
|
|
238
|
+
);
|
|
239
|
+
processed = processed.replace(/<\/forgewp-block-area>/g, '');
|
|
240
|
+
|
|
241
|
+
// ── Custom WP_Query Loop Blocks ──
|
|
242
|
+
processed = processed.replace(
|
|
243
|
+
/<forgewp-query-loop-start\s+([^>]*)\/?>/g,
|
|
244
|
+
(match, attrsStr) => {
|
|
245
|
+
const getAttr = (name) => {
|
|
246
|
+
const regex = new RegExp(
|
|
247
|
+
`(?:${name}|${name.toLowerCase()})=(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`,
|
|
248
|
+
'i',
|
|
249
|
+
);
|
|
250
|
+
const m = attrsStr.match(regex);
|
|
251
|
+
return m ? m[1] || m[2] || m[3] || '' : '';
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const postType = getAttr('postType') || getAttr('posttype') || 'post';
|
|
255
|
+
const postsPerPage =
|
|
256
|
+
getAttr('postsPerPage') || getAttr('postsperpage') || '10';
|
|
257
|
+
const categoryName =
|
|
258
|
+
getAttr('categoryName') || getAttr('categoryname') || '';
|
|
259
|
+
const metaKey = getAttr('metaKey') || getAttr('metakey') || '';
|
|
260
|
+
const metaValue = getAttr('metaValue') || getAttr('metavalue') || '';
|
|
261
|
+
const metaCompare = getAttr('metaCompare') || getAttr('metacompare') || '=';
|
|
262
|
+
const taxTaxonomy = getAttr('taxTaxonomy') || getAttr('taxtaxonomy') || '';
|
|
263
|
+
const taxTerms = getAttr('taxTerms') || getAttr('taxterms') || '';
|
|
264
|
+
const postIn = getAttr('postIn') || getAttr('postin') || '';
|
|
265
|
+
|
|
266
|
+
if (postIn !== '') {
|
|
267
|
+
return `<?php
|
|
268
|
+
$related_val = get_post_meta( get_the_ID(), '${postIn}', true );
|
|
269
|
+
$ids_arr = array_filter( array_map( 'intval', explode( ',', $related_val ) ) );
|
|
270
|
+
$query_args = array(
|
|
271
|
+
'post_type' => '${postType}',
|
|
272
|
+
'posts_per_page' => ${postsPerPage},
|
|
273
|
+
'post__in' => !empty($ids_arr) ? $ids_arr : array(-1),
|
|
274
|
+
'orderby' => 'post__in',
|
|
275
|
+
);
|
|
276
|
+
if (function_exists('pll_current_language')) {
|
|
277
|
+
$query_args['lang'] = pll_current_language();
|
|
278
|
+
}
|
|
279
|
+
$custom_query = new WP_Query($query_args);
|
|
280
|
+
if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();
|
|
281
|
+
?>`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let phpArgs = ` 'post_type' => '${postType}',\n 'posts_per_page' => ${postsPerPage},`;
|
|
285
|
+
|
|
286
|
+
if (categoryName !== '') {
|
|
287
|
+
phpArgs += `\n 'category_name' => '${categoryName}',`;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (metaKey !== '') {
|
|
291
|
+
let valExpr = `'${metaValue}'`;
|
|
292
|
+
if (metaValue === 'CURRENT_POST_ID') {
|
|
293
|
+
valExpr = 'get_the_ID()';
|
|
294
|
+
} else if (metaValue === ',CURRENT_POST_ID,') {
|
|
295
|
+
valExpr = "',' . get_the_ID() . ','";
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
phpArgs += `\n 'meta_query' => array(
|
|
299
|
+
array(
|
|
300
|
+
'key' => '${metaKey}',
|
|
301
|
+
'value' => ${valExpr},
|
|
302
|
+
'compare' => '${metaCompare}'
|
|
303
|
+
)
|
|
304
|
+
),`;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (taxTaxonomy !== '') {
|
|
308
|
+
phpArgs += `\n 'tax_query' => array(
|
|
309
|
+
array(
|
|
310
|
+
'taxonomy' => '${taxTaxonomy}',
|
|
311
|
+
'field' => 'slug',
|
|
312
|
+
'terms' => '${taxTerms}'
|
|
313
|
+
)
|
|
314
|
+
),`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return `<?php
|
|
318
|
+
$query_args = array(
|
|
319
|
+
${phpArgs}
|
|
320
|
+
);
|
|
321
|
+
if (function_exists('pll_current_language')) {
|
|
322
|
+
$query_args['lang'] = pll_current_language();
|
|
323
|
+
}
|
|
324
|
+
$custom_query = new WP_Query($query_args);
|
|
325
|
+
if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();
|
|
326
|
+
?>`;
|
|
327
|
+
},
|
|
328
|
+
);
|
|
329
|
+
processed = processed.replace(/<\/forgewp-query-loop-start>/g, '');
|
|
330
|
+
processed = processed.replace(
|
|
331
|
+
/<forgewp-query-loop-end\s*\/?>/g,
|
|
332
|
+
'<?php\n endwhile;\n wp_reset_postdata();\n endif;\n ?>',
|
|
333
|
+
);
|
|
334
|
+
processed = processed.replace(/<\/forgewp-query-loop-end>/g, '');
|
|
335
|
+
|
|
336
|
+
// ── Custom WpImage Primitives ──
|
|
337
|
+
processed = processed.replace(
|
|
338
|
+
/<forgewp-image\s+([^>]*)\/?>/g,
|
|
339
|
+
(match, attrsStr) => {
|
|
340
|
+
const getAttr = (name) => {
|
|
341
|
+
const regex = new RegExp(
|
|
342
|
+
`data-${name}=(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`,
|
|
343
|
+
);
|
|
344
|
+
const m = attrsStr.match(regex);
|
|
345
|
+
return m ? m[1] || m[2] || m[3] || '' : '';
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const id = getAttr('id');
|
|
349
|
+
const field = getAttr('field');
|
|
350
|
+
const size = getAttr('size') || 'full';
|
|
351
|
+
const className = getAttr('class-name');
|
|
352
|
+
const alt = getAttr('alt');
|
|
353
|
+
|
|
354
|
+
if (id) {
|
|
355
|
+
return `<?php echo wp_get_attachment_image( ${id}, '${size}', false, array( 'class' => '${className}', 'alt' => '${alt}' ) ); ?>`;
|
|
356
|
+
} else if (field === 'featuredImage') {
|
|
357
|
+
return `<?php echo wp_get_attachment_image( get_post_thumbnail_id( get_the_ID() ), '${size}', false, array( 'class' => '${className}', 'alt' => '${alt}' ) ); ?>`;
|
|
358
|
+
} else if (field) {
|
|
359
|
+
return `<?php
|
|
360
|
+
$img_val = get_post_meta( get_the_ID(), '${field}', true );
|
|
361
|
+
if ( is_numeric( $img_val ) ) {
|
|
362
|
+
echo wp_get_attachment_image( $img_val, '${size}', false, array( 'class' => '${className}', 'alt' => '${alt}' ) );
|
|
363
|
+
} elseif ( ! empty( $img_val ) ) {
|
|
364
|
+
echo '<img src="' . esc_url( $img_val ) . '" class="' . esc_attr( '${className}' ) . '" alt="' . esc_attr( '${alt}' ) . '" />';
|
|
365
|
+
}
|
|
366
|
+
?>`;
|
|
367
|
+
} else {
|
|
368
|
+
return `<img class="${className}" alt="${alt}" src="<?php echo esc_url( get_theme_file_uri( 'assets/image-placeholder.png' ) ); ?>" />`;
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
processed = processed.replace(/<\/forgewp-image>/g, '');
|
|
373
|
+
|
|
374
|
+
// ── Declarative WpRepeater Loops ──
|
|
375
|
+
processed = processed.replace(
|
|
376
|
+
/<forgewp-repeater-start\s+name="([^"]+)"\s+subfields="([^"]+)"\s*\/?>/g,
|
|
377
|
+
(match, name, subfields) => {
|
|
378
|
+
const subfieldsArrayStr = subfields
|
|
379
|
+
.split(',')
|
|
380
|
+
.map((f) => `'${f.trim()}'`)
|
|
381
|
+
.join(', ');
|
|
382
|
+
|
|
383
|
+
return `<?php
|
|
384
|
+
$repeater_key = '${name}';
|
|
385
|
+
$sub_fields = array(${subfieldsArrayStr});
|
|
386
|
+
$repeater_rows = forgewp_get_repeater_field($repeater_key, $sub_fields);
|
|
387
|
+
if (empty($repeater_rows)) {
|
|
388
|
+
$repeater_rows = array(array());
|
|
389
|
+
}
|
|
390
|
+
foreach ($repeater_rows as $row) {
|
|
391
|
+
?>`;
|
|
392
|
+
}
|
|
393
|
+
);
|
|
394
|
+
processed = processed.replace(/<\/forgewp-repeater-start>/g, '');
|
|
395
|
+
|
|
396
|
+
processed = processed.replace(/<forgewp-repeater-end\s*\/?>/g, '');
|
|
397
|
+
processed = processed.replace(/<\/forgewp-repeater-end>/g, '<?php } ?>');
|
|
398
|
+
|
|
399
|
+
// ── Dynamic WpIcon Custom SVGs ──
|
|
400
|
+
processed = processed.replace(
|
|
401
|
+
/<forgewp-icon-placeholder\s+([^>]*)\/?>/g,
|
|
402
|
+
(match, attrsStr) => {
|
|
403
|
+
const getAttr = (name) => {
|
|
404
|
+
const regex = new RegExp(
|
|
405
|
+
`(?:${name}|${name.toLowerCase()})=(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`,
|
|
406
|
+
'i',
|
|
407
|
+
);
|
|
408
|
+
const m = attrsStr.match(regex);
|
|
409
|
+
return m ? m[1] || m[2] || m[3] || '' : '';
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
const nameAttr = getAttr('name');
|
|
413
|
+
const provider = getAttr('provider') || 'lucide';
|
|
414
|
+
const className = getAttr('class') || getAttr('className') || '';
|
|
415
|
+
|
|
416
|
+
const fieldMatch = nameAttr.match(/__FORGEWP_REPEATER_FIELD_([a-zA-Z0-9_-]+)__/);
|
|
417
|
+
if (fieldMatch) {
|
|
418
|
+
const fieldName = fieldMatch[1];
|
|
419
|
+
return `<?php forgewp_render_theme_icon($row['${fieldName}'], '${className}', '${provider}'); ?>`;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return `<?php forgewp_render_theme_icon('${nameAttr}', '${className}', '${provider}'); ?>`;
|
|
423
|
+
}
|
|
424
|
+
);
|
|
425
|
+
processed = processed.replace(/<\/forgewp-icon-placeholder>/g, '');
|
|
426
|
+
|
|
427
|
+
processed = processed.replace(
|
|
428
|
+
/__FORGEWP_REPEATER_FIELD_([a-zA-Z0-9_-]+)__/g,
|
|
429
|
+
(match, fieldName) => `<?php echo esc_html($row['${fieldName}']); ?>`
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
// ── Page Links Support ──
|
|
433
|
+
processed = processed.replace(
|
|
434
|
+
/__FORGEWP_PAGELINK_([a-zA-Z0-9_-]+)_DEFAULT_(.*?)__/g,
|
|
435
|
+
(match, name, defaultVal) => {
|
|
436
|
+
const decoded = decodeURIComponent(defaultVal);
|
|
437
|
+
return `<?php
|
|
438
|
+
$current_lang = function_exists('pll_current_language') ? pll_current_language() : '';
|
|
439
|
+
$get_pages_args = array('meta_key' => '_wp_page_template', 'meta_value' => 'page-${name}.php', 'number' => 1);
|
|
440
|
+
if (!empty($current_lang)) {
|
|
441
|
+
$get_pages_args['lang'] = $current_lang;
|
|
442
|
+
}
|
|
443
|
+
$matched_pages = get_pages($get_pages_args);
|
|
444
|
+
echo esc_url(!empty($matched_pages) ? get_permalink($matched_pages[0]->ID) : '${decoded.replace(/'/g, "\\'")}');
|
|
445
|
+
?>`;
|
|
446
|
+
},
|
|
447
|
+
);
|
|
448
|
+
processed = processed.replace(
|
|
449
|
+
/__FORGEWP_PAGELINK_([a-zA-Z0-9_-]+)__/g,
|
|
450
|
+
(match, name) => {
|
|
451
|
+
return `<?php
|
|
452
|
+
$current_lang = function_exists('pll_current_language') ? pll_current_language() : '';
|
|
453
|
+
$get_pages_args = array('meta_key' => '_wp_page_template', 'meta_value' => 'page-${name}.php', 'number' => 1);
|
|
454
|
+
if (!empty($current_lang)) {
|
|
455
|
+
$get_pages_args['lang'] = $current_lang;
|
|
456
|
+
}
|
|
457
|
+
$matched_pages = get_pages($get_pages_args);
|
|
458
|
+
echo esc_url(!empty($matched_pages) ? get_permalink($matched_pages[0]->ID) : '');
|
|
459
|
+
?>`;
|
|
460
|
+
},
|
|
461
|
+
);
|
|
462
|
+
|
|
463
|
+
// ── Auto-hide social media container if no social links exist ──
|
|
464
|
+
let searchIndex = 0;
|
|
465
|
+
while (true) {
|
|
466
|
+
const matchStart = processed.indexOf('data-forgewp-hide-empty-socials', searchIndex);
|
|
467
|
+
if (matchStart === -1) {
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
const divStart = processed.lastIndexOf('<div', matchStart);
|
|
471
|
+
if (divStart === -1) {
|
|
472
|
+
searchIndex = matchStart + 31;
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
const openTagEnd = processed.indexOf('>', matchStart);
|
|
476
|
+
if (openTagEnd === -1) {
|
|
477
|
+
searchIndex = matchStart + 31;
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
const contentStartIndex = openTagEnd + 1;
|
|
481
|
+
const matchEnd = findMatchingClosingDiv(processed, contentStartIndex);
|
|
482
|
+
if (matchEnd !== -1) {
|
|
483
|
+
const fullBlock = processed.substring(divStart, matchEnd);
|
|
484
|
+
const optionMatches = Array.from(fullBlock.matchAll(/__FORGEWP_OPTION_(social_[a-zA-Z0-9_-]+)__/g));
|
|
485
|
+
const optionNames = Array.from(new Set(optionMatches.map(m => m[1])));
|
|
486
|
+
const cleanBlock = fullBlock.replace(/\s*data-forgewp-hide-empty-socials="[^"]*"/gi, '').replace(/\s*data-forgewp-hide-empty-socials/gi, '');
|
|
487
|
+
let replacement = cleanBlock;
|
|
488
|
+
if (optionNames.length > 0) {
|
|
489
|
+
const conditions = optionNames.map(name => `! empty( get_option( '${name}' ) )`).join(' || ');
|
|
490
|
+
replacement = `<?php if ( ${conditions} ) : ?>${cleanBlock}<?php endif; ?>`;
|
|
491
|
+
}
|
|
492
|
+
processed = processed.substring(0, divStart) + replacement + processed.substring(matchEnd);
|
|
493
|
+
searchIndex = divStart + replacement.length;
|
|
494
|
+
} else {
|
|
495
|
+
searchIndex = matchStart + 31;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// ── Auto-hide empty social links / options ──
|
|
500
|
+
// If an <a> tag has href="__FORGEWP_OPTION_social_xxx__" and the option is empty, wrap it in a PHP conditional block.
|
|
501
|
+
processed = processed.replace(
|
|
502
|
+
/<a\b([^>]*?href=["']__FORGEWP_OPTION_([a-zA-Z0-9_-]+)__["'][^>]*?)>([\s\S]*?)<\/a>/gi,
|
|
503
|
+
(match, attributes, name, content) => {
|
|
504
|
+
if (name.startsWith('social_')) {
|
|
505
|
+
const cleanAttrs = attributes.replace(`__FORGEWP_OPTION_${name}__`, `<?php echo esc_url( get_option( '${name}' ) ); ?>`);
|
|
506
|
+
return `<?php if ( ! empty( get_option( '${name}' ) ) ) : ?><a${cleanAttrs}>${content}</a><?php endif; ?>`;
|
|
507
|
+
}
|
|
508
|
+
return match;
|
|
509
|
+
}
|
|
510
|
+
);
|
|
511
|
+
|
|
512
|
+
processed = processed.replace(
|
|
513
|
+
/<a\b([^>]*?href=["']__FORGEWP_OPTION_([a-zA-Z0-9_-]+)_DEFAULT_(.*?)__["'][^>]*?)>([\s\S]*?)<\/a>/gi,
|
|
514
|
+
(match, attributes, name, defaultVal, content) => {
|
|
515
|
+
if (name.startsWith('social_')) {
|
|
516
|
+
const decoded = decodeURIComponent(defaultVal);
|
|
517
|
+
const cleanAttrs = attributes.replace(`__FORGEWP_OPTION_${name}_DEFAULT_${defaultVal}__`, `<?php echo esc_url( get_option( '${name}', '${decoded}' ) ); ?>`);
|
|
518
|
+
return `<?php if ( ! empty( get_option( '${name}' ) ) ) : ?><a${cleanAttrs}>${content}</a><?php endif; ?>`;
|
|
519
|
+
}
|
|
520
|
+
return match;
|
|
521
|
+
}
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
// ── Options & Customizer Theme Mods Support ──
|
|
525
|
+
processed = processed.replace(
|
|
526
|
+
/__FORGEWP_OPTION_([a-zA-Z0-9_-]+)_DEFAULT_(.*?)__/g,
|
|
527
|
+
(match, name, defaultVal) => {
|
|
528
|
+
const decoded = decodeURIComponent(defaultVal);
|
|
529
|
+
if (name === 'blogname') {
|
|
530
|
+
return `<?php echo esc_html( get_bloginfo( 'name' ) ); ?>`;
|
|
531
|
+
}
|
|
532
|
+
if (name === 'blogdescription') {
|
|
533
|
+
return `<?php echo esc_html( get_bloginfo( 'description' ) ); ?>`;
|
|
534
|
+
}
|
|
535
|
+
return `<?php echo esc_html( get_option( '${name}', '${decoded}' ) ); ?>`;
|
|
536
|
+
},
|
|
537
|
+
);
|
|
538
|
+
processed = processed.replace(
|
|
539
|
+
/__FORGEWP_OPTION_([a-zA-Z0-9_-]+)__/g,
|
|
540
|
+
(match, name) => {
|
|
541
|
+
if (name === 'blogname') {
|
|
542
|
+
return `<?php echo esc_html( get_bloginfo( 'name' ) ); ?>`;
|
|
543
|
+
}
|
|
544
|
+
if (name === 'blogdescription') {
|
|
545
|
+
return `<?php echo esc_html( get_bloginfo( 'description' ) ); ?>`;
|
|
546
|
+
}
|
|
547
|
+
return `<?php echo esc_html( get_option( '${name}' ) ); ?>`;
|
|
548
|
+
},
|
|
549
|
+
);
|
|
550
|
+
|
|
551
|
+
processed = processed.replace(
|
|
552
|
+
/__FORGEWP_THEME_MOD_([a-zA-Z0-9_-]+)_DEFAULT_(.*?)__/g,
|
|
553
|
+
(match, name, defaultVal) => {
|
|
554
|
+
const decoded = decodeURIComponent(defaultVal);
|
|
555
|
+
return `<?php echo esc_html( get_theme_mod( '${name}', '${decoded}' ) ); ?>`;
|
|
556
|
+
},
|
|
557
|
+
);
|
|
558
|
+
processed = processed.replace(
|
|
559
|
+
/__FORGEWP_THEME_MOD_([a-zA-Z0-9_-]+)__/g,
|
|
560
|
+
"<?php echo esc_html( get_theme_mod( '$1' ) ); ?>",
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
processed = processed.replace(
|
|
564
|
+
/__FORGEWP_THEME_URI__/g,
|
|
565
|
+
"<?php echo esc_url( get_template_directory_uri() ); ?>",
|
|
566
|
+
);
|
|
567
|
+
|
|
568
|
+
return processed;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function findMatchingClosingDiv(html, startIndex) {
|
|
572
|
+
let openCount = 1;
|
|
573
|
+
let index = startIndex;
|
|
574
|
+
while (openCount > 0 && index < html.length) {
|
|
575
|
+
const nextOpen = html.indexOf('<div', index);
|
|
576
|
+
const nextClose = html.indexOf('</div>', index);
|
|
577
|
+
if (nextClose === -1) {
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
if (nextOpen !== -1 && nextOpen < nextClose) {
|
|
581
|
+
openCount++;
|
|
582
|
+
index = nextOpen + 4;
|
|
583
|
+
} else {
|
|
584
|
+
openCount--;
|
|
585
|
+
index = nextClose + 6;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return openCount === 0 ? index : -1;
|
|
589
|
+
}
|