@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
package/lib/functions-builder.js
DELETED
|
@@ -1,2970 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import {
|
|
4
|
-
generateAcfFieldPhp,
|
|
5
|
-
generateDynamicAcfFieldPhp,
|
|
6
|
-
} from './php-builders.js';
|
|
7
|
-
|
|
8
|
-
function generatePhpArgs(queryObj) {
|
|
9
|
-
const phpLines = [];
|
|
10
|
-
phpLines.push(`$args = array(`);
|
|
11
|
-
|
|
12
|
-
if (queryObj.postType && typeof queryObj.postType === 'string') {
|
|
13
|
-
phpLines.push(` 'post_type' => '${queryObj.postType}',`);
|
|
14
|
-
} else {
|
|
15
|
-
phpLines.push(` 'post_type' => 'post',`);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (queryObj.postsPerPage !== undefined) {
|
|
19
|
-
const ppp = parseInt(queryObj.postsPerPage, 10);
|
|
20
|
-
phpLines.push(` 'posts_per_page' => ${isNaN(ppp) ? 10 : ppp},`);
|
|
21
|
-
} else {
|
|
22
|
-
phpLines.push(` 'posts_per_page' => 10,`);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (queryObj.orderby) {
|
|
26
|
-
phpLines.push(` 'orderby' => '${queryObj.orderby}',`);
|
|
27
|
-
}
|
|
28
|
-
if (queryObj.order) {
|
|
29
|
-
phpLines.push(` 'order' => '${queryObj.order}',`);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (queryObj.categoryName) {
|
|
33
|
-
if (String(queryObj.categoryName).startsWith('__DYNAMIC_')) {
|
|
34
|
-
const varName = String(queryObj.categoryName).replace(/^__DYNAMIC_/, '').replace(/__$/, '');
|
|
35
|
-
phpLines.push(` 'category_name' => $request->get_param('${varName}') ? sanitize_text_field($request->get_param('${varName}')) : '',`);
|
|
36
|
-
} else {
|
|
37
|
-
phpLines.push(` 'category_name' => '${queryObj.categoryName}',`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
phpLines.push(` 'paged' => $paged,`);
|
|
42
|
-
|
|
43
|
-
if (queryObj.taxQuery && Array.isArray(queryObj.taxQuery) && queryObj.taxQuery.length > 0) {
|
|
44
|
-
phpLines.push(` 'tax_query' => array(`);
|
|
45
|
-
for (const taxQ of queryObj.taxQuery) {
|
|
46
|
-
phpLines.push(` array(`);
|
|
47
|
-
phpLines.push(` 'taxonomy' => '${taxQ.taxonomy}',`);
|
|
48
|
-
phpLines.push(` 'field' => '${taxQ.field || 'slug'}',`);
|
|
49
|
-
|
|
50
|
-
const termsVal = taxQ.terms;
|
|
51
|
-
if (typeof termsVal === 'string' && termsVal.startsWith('__DYNAMIC_')) {
|
|
52
|
-
const varName = termsVal.replace(/^__DYNAMIC_/, '').replace(/__$/, '');
|
|
53
|
-
phpLines.push(` 'terms' => $request->get_param('${varName}') ? sanitize_text_field($request->get_param('${varName}')) : '',`);
|
|
54
|
-
} else if (Array.isArray(termsVal)) {
|
|
55
|
-
const termsPhp = termsVal.map(t => typeof t === 'string' ? `'${t}'` : t).join(', ');
|
|
56
|
-
phpLines.push(` 'terms' => array(${termsPhp}),`);
|
|
57
|
-
} else if (typeof termsVal === 'string') {
|
|
58
|
-
phpLines.push(` 'terms' => '${termsVal}',`);
|
|
59
|
-
} else {
|
|
60
|
-
phpLines.push(` 'terms' => ${termsVal},`);
|
|
61
|
-
}
|
|
62
|
-
phpLines.push(` ),`);
|
|
63
|
-
}
|
|
64
|
-
phpLines.push(` ),`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (queryObj.metaQuery && Array.isArray(queryObj.metaQuery) && queryObj.metaQuery.length > 0) {
|
|
68
|
-
phpLines.push(` 'meta_query' => array(`);
|
|
69
|
-
if (queryObj.metaRelation) {
|
|
70
|
-
phpLines.push(` 'relation' => '${queryObj.metaRelation}',`);
|
|
71
|
-
}
|
|
72
|
-
for (const metaQ of queryObj.metaQuery) {
|
|
73
|
-
phpLines.push(` array(`);
|
|
74
|
-
phpLines.push(` 'key' => '${metaQ.key}',`);
|
|
75
|
-
if (metaQ.compare) {
|
|
76
|
-
phpLines.push(` 'compare' => '${metaQ.compare}',`);
|
|
77
|
-
}
|
|
78
|
-
if (metaQ.value !== undefined) {
|
|
79
|
-
const val = metaQ.value;
|
|
80
|
-
if (typeof val === 'string' && val.startsWith('__DYNAMIC_')) {
|
|
81
|
-
const varName = val.replace(/^__DYNAMIC_/, '').replace(/__$/, '');
|
|
82
|
-
phpLines.push(` 'value' => $request->get_param('${varName}') ? sanitize_text_field($request->get_param('${varName}')) : '',`);
|
|
83
|
-
} else if (typeof val === 'string') {
|
|
84
|
-
phpLines.push(` 'value' => '${val}',`);
|
|
85
|
-
} else {
|
|
86
|
-
phpLines.push(` 'value' => ${val},`);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
phpLines.push(` ),`);
|
|
90
|
-
}
|
|
91
|
-
phpLines.push(` ),`);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
phpLines.push(` );`);
|
|
95
|
-
return phpLines.join('\n');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Generate functions.php template string
|
|
100
|
-
*
|
|
101
|
-
* @param {import('./types.js').ForgeWPThemeConfig} config
|
|
102
|
-
* @param {import('./types.js').ForgeWPBuildAssets} assets
|
|
103
|
-
*/
|
|
104
|
-
export function buildFunctionsPhp(
|
|
105
|
-
config,
|
|
106
|
-
assets,
|
|
107
|
-
blockSlugs = [],
|
|
108
|
-
themeRoot = '',
|
|
109
|
-
pagesToAutoCreate = [],
|
|
110
|
-
menus = {},
|
|
111
|
-
hydrationData = null,
|
|
112
|
-
i18nKeys = [],
|
|
113
|
-
schemas = {},
|
|
114
|
-
queries = [],
|
|
115
|
-
) {
|
|
116
|
-
let hasAuth = false;
|
|
117
|
-
try {
|
|
118
|
-
const packageJsonPath = path.join(themeRoot, 'package.json');
|
|
119
|
-
if (existsSync(packageJsonPath)) {
|
|
120
|
-
const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
|
|
121
|
-
if (
|
|
122
|
-
(pkg.dependencies && pkg.dependencies['@forgewp/auth']) ||
|
|
123
|
-
(pkg.devDependencies && pkg.devDependencies['@forgewp/auth']) ||
|
|
124
|
-
(pkg.peerDependencies && pkg.peerDependencies['@forgewp/auth'])
|
|
125
|
-
) {
|
|
126
|
-
hasAuth = true;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
} catch (e) {}
|
|
130
|
-
|
|
131
|
-
const defaultLoginField = config.auth?.loginField || 'usernameAndEmail';
|
|
132
|
-
const defaultRegistrationEnabled = config.auth?.features?.registration !== false ? '1' : '0';
|
|
133
|
-
const defaultEmailVerificationEnabled = config.auth?.features?.emailVerification === true ? '1' : '0';
|
|
134
|
-
const defaultBlockLoginUntilVerified = config.auth?.features?.blockLoginUntilVerified === true ? '1' : '0';
|
|
135
|
-
const defaultAutoLoginAfterSignup = config.auth?.features?.autoLoginAfterSignup !== false ? '1' : '0';
|
|
136
|
-
const defaultDefaultRole = config.auth?.defaultRole || 'subscriber';
|
|
137
|
-
const defaultVerificationEmailSubject = config.auth?.emails?.verification?.subject || 'Please Verify Your Email';
|
|
138
|
-
const defaultVerificationEmailBody = config.auth?.emails?.verification?.body || 'Please click the link below to verify your email address:\n\n{verification_url}';
|
|
139
|
-
const defaultPasswordResetEmailSubject = config.auth?.emails?.passwordReset?.subject || 'Password Reset Request';
|
|
140
|
-
const defaultPasswordResetEmailBody = config.auth?.emails?.passwordReset?.body || 'Click this link to reset your password:\n\n{reset_url}';
|
|
141
|
-
|
|
142
|
-
const reservedUsernames = config.auth?.reservedUsernames || ['admin', 'system', 'root'];
|
|
143
|
-
const reservedUsernamesPhp = reservedUsernames.map(u => `'${u.toLowerCase().replace(/'/g, "\\'")}'`).join(', ');
|
|
144
|
-
|
|
145
|
-
let currentUserHydrationField = '';
|
|
146
|
-
let authControllersPhp = '';
|
|
147
|
-
|
|
148
|
-
if (hasAuth) {
|
|
149
|
-
currentUserHydrationField = "\n 'currentUser' => forgewp_get_current_user_hydration_payload(),";
|
|
150
|
-
authControllersPhp = `
|
|
151
|
-
/**
|
|
152
|
-
* ── ForgeWP REST Authentication Controllers ─────────────────────────────────
|
|
153
|
-
*/
|
|
154
|
-
|
|
155
|
-
function forgewp_get_current_user_hydration_payload() {
|
|
156
|
-
if ( ! is_user_logged_in() ) {
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
159
|
-
$user = wp_get_current_user();
|
|
160
|
-
if ( ! $user || $user->ID === 0 ) {
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
$capabilities = array();
|
|
164
|
-
if ( is_array( $user->allcaps ) ) {
|
|
165
|
-
foreach ( $user->allcaps as $cap => $value ) {
|
|
166
|
-
if ( $value ) {
|
|
167
|
-
$capabilities[] = $cap;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
$verified_meta = get_user_meta( $user->ID, 'forgewp_email_verified', true );
|
|
173
|
-
$email_verified = ( $verified_meta === '' ) || ( $verified_meta === '1' ) || ( $verified_meta === true );
|
|
174
|
-
|
|
175
|
-
return array(
|
|
176
|
-
'id' => $user->ID,
|
|
177
|
-
'username' => $user->user_login,
|
|
178
|
-
'email' => $user->user_email,
|
|
179
|
-
'displayName' => $user->display_name,
|
|
180
|
-
'roles' => array_values( $user->roles ),
|
|
181
|
-
'avatarUrl' => get_avatar_url( $user->ID ),
|
|
182
|
-
'capabilities' => $capabilities,
|
|
183
|
-
'emailVerified' => $email_verified,
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Register dynamic authentication settings in WP Admin Settings Submenu page.
|
|
189
|
-
*/
|
|
190
|
-
add_action( 'admin_menu', 'forgewp_auth_admin_menu' );
|
|
191
|
-
function forgewp_auth_admin_menu() {
|
|
192
|
-
add_options_page(
|
|
193
|
-
'ForgeWP Auth Settings',
|
|
194
|
-
'ForgeWP Auth',
|
|
195
|
-
'manage_options',
|
|
196
|
-
'forgewp-auth-settings',
|
|
197
|
-
'forgewp_auth_settings_page_render'
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function forgewp_auth_settings_page_render() {
|
|
202
|
-
if ( ! current_user_can( 'manage_options' ) ) {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
?>
|
|
206
|
-
<div class="wrap">
|
|
207
|
-
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
|
|
208
|
-
<form action="options.php" method="post">
|
|
209
|
-
<?php
|
|
210
|
-
settings_fields( 'forgewp_auth_settings_group' );
|
|
211
|
-
do_settings_sections( 'forgewp-auth-settings' );
|
|
212
|
-
submit_button( 'Save Settings' );
|
|
213
|
-
?>
|
|
214
|
-
</form>
|
|
215
|
-
</div>
|
|
216
|
-
<?php
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
add_action( 'admin_init', 'forgewp_register_auth_settings' );
|
|
220
|
-
function forgewp_register_auth_settings() {
|
|
221
|
-
// Keep forgewp_auth_login_field on general for backward compatibility
|
|
222
|
-
register_setting( 'general', 'forgewp_auth_login_field', array(
|
|
223
|
-
'type' => 'string',
|
|
224
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
225
|
-
'default' => '${defaultLoginField}',
|
|
226
|
-
) );
|
|
227
|
-
|
|
228
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_login_field', array(
|
|
229
|
-
'type' => 'string',
|
|
230
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
231
|
-
'default' => '${defaultLoginField}',
|
|
232
|
-
) );
|
|
233
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_registration_enabled', array(
|
|
234
|
-
'type' => 'string',
|
|
235
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
236
|
-
'default' => '${defaultRegistrationEnabled}',
|
|
237
|
-
) );
|
|
238
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_email_verification_enabled', array(
|
|
239
|
-
'type' => 'string',
|
|
240
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
241
|
-
'default' => '${defaultEmailVerificationEnabled}',
|
|
242
|
-
) );
|
|
243
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_block_login_until_verified', array(
|
|
244
|
-
'type' => 'string',
|
|
245
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
246
|
-
'default' => '${defaultBlockLoginUntilVerified}',
|
|
247
|
-
) );
|
|
248
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_default_role', array(
|
|
249
|
-
'type' => 'string',
|
|
250
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
251
|
-
'default' => '${defaultDefaultRole}',
|
|
252
|
-
) );
|
|
253
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_verification_email_subject', array(
|
|
254
|
-
'type' => 'string',
|
|
255
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
256
|
-
'default' => '${defaultVerificationEmailSubject.replace(/'/g, "\\'")}',
|
|
257
|
-
) );
|
|
258
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_verification_email_body', array(
|
|
259
|
-
'type' => 'string',
|
|
260
|
-
'sanitize_callback' => 'sanitize_textarea_field',
|
|
261
|
-
'default' => '${defaultVerificationEmailBody.replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}',
|
|
262
|
-
) );
|
|
263
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_password_reset_email_subject', array(
|
|
264
|
-
'type' => 'string',
|
|
265
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
266
|
-
'default' => '${defaultPasswordResetEmailSubject.replace(/'/g, "\\'")}',
|
|
267
|
-
) );
|
|
268
|
-
register_setting( 'forgewp_auth_settings_group', 'forgewp_auth_password_reset_email_body', array(
|
|
269
|
-
'type' => 'string',
|
|
270
|
-
'sanitize_callback' => 'sanitize_textarea_field',
|
|
271
|
-
'default' => '${defaultPasswordResetEmailBody.replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}',
|
|
272
|
-
) );
|
|
273
|
-
|
|
274
|
-
add_settings_section(
|
|
275
|
-
'forgewp_auth_settings_section',
|
|
276
|
-
'Authentication & Registration Configurations',
|
|
277
|
-
'forgewp_auth_settings_section_render',
|
|
278
|
-
'forgewp-auth-settings'
|
|
279
|
-
);
|
|
280
|
-
|
|
281
|
-
add_settings_field(
|
|
282
|
-
'forgewp_auth_login_field',
|
|
283
|
-
'Allowed Login Fields',
|
|
284
|
-
'forgewp_auth_login_field_render',
|
|
285
|
-
'forgewp-auth-settings',
|
|
286
|
-
'forgewp_auth_settings_section'
|
|
287
|
-
);
|
|
288
|
-
add_settings_field(
|
|
289
|
-
'forgewp_auth_registration_enabled',
|
|
290
|
-
'Enable Public Registration',
|
|
291
|
-
'forgewp_auth_registration_enabled_render',
|
|
292
|
-
'forgewp-auth-settings',
|
|
293
|
-
'forgewp_auth_settings_section'
|
|
294
|
-
);
|
|
295
|
-
add_settings_field(
|
|
296
|
-
'forgewp_auth_email_verification_enabled',
|
|
297
|
-
'Require Email Verification',
|
|
298
|
-
'forgewp_auth_email_verification_enabled_render',
|
|
299
|
-
'forgewp-auth-settings',
|
|
300
|
-
'forgewp_auth_settings_section'
|
|
301
|
-
);
|
|
302
|
-
add_settings_field(
|
|
303
|
-
'forgewp_auth_block_login_until_verified',
|
|
304
|
-
'Block Login for Unverified Users',
|
|
305
|
-
'forgewp_auth_block_login_until_verified_render',
|
|
306
|
-
'forgewp-auth-settings',
|
|
307
|
-
'forgewp_auth_settings_section'
|
|
308
|
-
);
|
|
309
|
-
add_settings_field(
|
|
310
|
-
'forgewp_auth_default_role',
|
|
311
|
-
'Default User Role',
|
|
312
|
-
'forgewp_auth_default_role_render',
|
|
313
|
-
'forgewp-auth-settings',
|
|
314
|
-
'forgewp_auth_settings_section'
|
|
315
|
-
);
|
|
316
|
-
add_settings_field(
|
|
317
|
-
'forgewp_auth_verification_email_subject',
|
|
318
|
-
'Verification Email Subject',
|
|
319
|
-
'forgewp_auth_verification_email_subject_render',
|
|
320
|
-
'forgewp-auth-settings',
|
|
321
|
-
'forgewp_auth_settings_section'
|
|
322
|
-
);
|
|
323
|
-
add_settings_field(
|
|
324
|
-
'forgewp_auth_verification_email_body',
|
|
325
|
-
'Verification Email Body',
|
|
326
|
-
'forgewp_auth_verification_email_body_render',
|
|
327
|
-
'forgewp-auth-settings',
|
|
328
|
-
'forgewp_auth_settings_section'
|
|
329
|
-
);
|
|
330
|
-
add_settings_field(
|
|
331
|
-
'forgewp_auth_password_reset_email_subject',
|
|
332
|
-
'Password Reset Email Subject',
|
|
333
|
-
'forgewp_auth_password_reset_email_subject_render',
|
|
334
|
-
'forgewp-auth-settings',
|
|
335
|
-
'forgewp_auth_settings_section'
|
|
336
|
-
);
|
|
337
|
-
add_settings_field(
|
|
338
|
-
'forgewp_auth_password_reset_email_body',
|
|
339
|
-
'Password Reset Email Body',
|
|
340
|
-
'forgewp_auth_password_reset_email_body_render',
|
|
341
|
-
'forgewp-auth-settings',
|
|
342
|
-
'forgewp_auth_settings_section'
|
|
343
|
-
);
|
|
344
|
-
|
|
345
|
-
add_settings_field(
|
|
346
|
-
'forgewp_auth_login_field',
|
|
347
|
-
'Allowed Login Fields',
|
|
348
|
-
'forgewp_auth_login_field_render',
|
|
349
|
-
'general'
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// Keep WordPress General Membership and ForgeWP Auth Public Registration in sync
|
|
354
|
-
function forgewp_sync_registration_options( $option, $value ) {
|
|
355
|
-
static $in_sync = false;
|
|
356
|
-
if ( $in_sync ) {
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
$in_sync = true;
|
|
360
|
-
if ( $option === 'forgewp_auth_registration_enabled' ) {
|
|
361
|
-
update_option( 'users_can_register', $value );
|
|
362
|
-
} elseif ( $option === 'users_can_register' ) {
|
|
363
|
-
update_option( 'forgewp_auth_registration_enabled', $value );
|
|
364
|
-
}
|
|
365
|
-
$in_sync = false;
|
|
366
|
-
}
|
|
367
|
-
add_action( 'update_option_forgewp_auth_registration_enabled', function($old, $new) { forgewp_sync_registration_options('forgewp_auth_registration_enabled', $new); }, 10, 2 );
|
|
368
|
-
add_action( 'update_option_users_can_register', function($old, $new) { forgewp_sync_registration_options('users_can_register', $new); }, 10, 2 );
|
|
369
|
-
add_action( 'add_option_forgewp_auth_registration_enabled', function($option, $value) { forgewp_sync_registration_options('forgewp_auth_registration_enabled', $value); }, 10, 2 );
|
|
370
|
-
add_action( 'add_option_users_can_register', function($option, $value) { forgewp_sync_registration_options('users_can_register', $value); }, 10, 2 );
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
* Display custom fields dynamically on User Profile edit screen in wp-admin
|
|
374
|
-
*/
|
|
375
|
-
function forgewp_show_custom_user_profile_fields( $user ) {
|
|
376
|
-
$keys = get_option( 'forgewp_registered_user_meta_keys', array() );
|
|
377
|
-
if ( empty( $keys ) ) {
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
?>
|
|
381
|
-
<h3>ForgeWP Custom Profile Details</h3>
|
|
382
|
-
<table class="form-table">
|
|
383
|
-
<?php foreach ( $keys as $key ) :
|
|
384
|
-
$clean_key = sanitize_key( $key );
|
|
385
|
-
$label = ucwords( str_replace( array( '_', '-' ), ' ', $clean_key ) );
|
|
386
|
-
$value = get_user_meta( $user->ID, $clean_key, true );
|
|
387
|
-
?>
|
|
388
|
-
<tr>
|
|
389
|
-
<th><label for="<?php echo esc_attr( $clean_key ); ?>"><?php echo esc_html( $label ); ?></label></th>
|
|
390
|
-
<td>
|
|
391
|
-
<input type="text" name="<?php echo esc_attr( $clean_key ); ?>" id="<?php echo esc_attr( $clean_key ); ?>" value="<?php echo esc_attr( $value ); ?>" class="regular-text" />
|
|
392
|
-
</td>
|
|
393
|
-
</tr>
|
|
394
|
-
<?php endforeach; ?>
|
|
395
|
-
</table>
|
|
396
|
-
<?php
|
|
397
|
-
}
|
|
398
|
-
add_action( 'show_user_profile', 'forgewp_show_custom_user_profile_fields' );
|
|
399
|
-
add_action( 'edit_user_profile', 'forgewp_show_custom_user_profile_fields' );
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Save custom fields dynamically from User Profile edit screen in wp-admin
|
|
403
|
-
*/
|
|
404
|
-
function forgewp_save_custom_user_profile_fields( $user_id ) {
|
|
405
|
-
if ( ! current_user_can( 'edit_user', $user_id ) ) {
|
|
406
|
-
return false;
|
|
407
|
-
}
|
|
408
|
-
$keys = get_option( 'forgewp_registered_user_meta_keys', array() );
|
|
409
|
-
if ( empty( $keys ) ) {
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
foreach ( $keys as $key ) {
|
|
413
|
-
$clean_key = sanitize_key( $key );
|
|
414
|
-
if ( isset( $_POST[ $clean_key ] ) ) {
|
|
415
|
-
update_user_meta( $user_id, $clean_key, sanitize_text_field( $_POST[ $clean_key ] ) );
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
add_action( 'personal_options_update', 'forgewp_save_custom_user_profile_fields' );
|
|
420
|
-
add_action( 'edit_user_profile_update', 'forgewp_save_custom_user_profile_fields' );
|
|
421
|
-
|
|
422
|
-
function forgewp_auth_settings_section_render() {
|
|
423
|
-
echo '<p>Configure settings for ForgeWP authentication and registration.</p>';
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
function forgewp_auth_login_field_render() {
|
|
427
|
-
$value = get_option( 'forgewp_auth_login_field', '${defaultLoginField}' );
|
|
428
|
-
?>
|
|
429
|
-
<select name="forgewp_auth_login_field">
|
|
430
|
-
<option value="usernameAndEmail" <?php selected( $value, 'usernameAndEmail' ); ?>>Username or Email (Default)</option>
|
|
431
|
-
<option value="usernameOnly" <?php selected( $value, 'usernameOnly' ); ?>>Username Only</option>
|
|
432
|
-
<option value="emailOnly" <?php selected( $value, 'emailOnly' ); ?>>Email Only</option>
|
|
433
|
-
</select>
|
|
434
|
-
<p class="description">Select which credentials users can log in with.</p>
|
|
435
|
-
<?php
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
function forgewp_auth_registration_enabled_render() {
|
|
439
|
-
$value = get_option( 'forgewp_auth_registration_enabled', '${defaultRegistrationEnabled}' );
|
|
440
|
-
?>
|
|
441
|
-
<input type="checkbox" name="forgewp_auth_registration_enabled" value="1" <?php checked( $value, '1' ); ?> />
|
|
442
|
-
<span class="description">Allow new users to sign up via registration API.</span>
|
|
443
|
-
<?php
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
function forgewp_auth_email_verification_enabled_render() {
|
|
447
|
-
$value = get_option( 'forgewp_auth_email_verification_enabled', '${defaultEmailVerificationEnabled}' );
|
|
448
|
-
?>
|
|
449
|
-
<input type="checkbox" name="forgewp_auth_email_verification_enabled" value="1" <?php checked( $value, '1' ); ?> />
|
|
450
|
-
<span class="description">Require new users to click verification links in email.</span>
|
|
451
|
-
<?php
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
function forgewp_auth_block_login_until_verified_render() {
|
|
455
|
-
$value = get_option( 'forgewp_auth_block_login_until_verified', '${defaultBlockLoginUntilVerified}' );
|
|
456
|
-
?>
|
|
457
|
-
<input type="checkbox" name="forgewp_auth_block_login_until_verified" value="1" <?php checked( $value, '1' ); ?> />
|
|
458
|
-
<span class="description">Prevent users from logging in unless their email address is verified.</span>
|
|
459
|
-
<?php
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
// Intercept logins to block unverified email addresses if option is enabled
|
|
463
|
-
add_filter( 'wp_authenticate_user', 'forgewp_auth_check_verification_before_login', 30, 2 );
|
|
464
|
-
function forgewp_auth_check_verification_before_login( $user, $password ) {
|
|
465
|
-
if ( is_wp_error( $user ) ) {
|
|
466
|
-
return $user;
|
|
467
|
-
}
|
|
468
|
-
$block_login = get_option( 'forgewp_auth_block_login_until_verified', '${defaultBlockLoginUntilVerified}' ) === '1';
|
|
469
|
-
if ( $block_login ) {
|
|
470
|
-
$email_verified = get_user_meta( $user->ID, 'forgewp_email_verified', true );
|
|
471
|
-
if ( $email_verified !== '1' ) {
|
|
472
|
-
return new WP_Error( 'email_unverified', 'Your email address has not been verified yet. Please check your inbox for the verification link.', array( 'status' => 403 ) );
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
return $user;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
function forgewp_auth_default_role_render() {
|
|
479
|
-
$value = get_option( 'forgewp_auth_default_role', '${defaultDefaultRole}' );
|
|
480
|
-
$roles = wp_roles()->get_names();
|
|
481
|
-
?>
|
|
482
|
-
<select name="forgewp_auth_default_role">
|
|
483
|
-
<?php foreach ( $roles as $role_key => $role_name ) : ?>
|
|
484
|
-
<option value="<?php echo esc_attr( $role_key ); ?>" <?php selected( $value, $role_key ); ?>>
|
|
485
|
-
<?php echo esc_html( $role_name ); ?>
|
|
486
|
-
</option>
|
|
487
|
-
<?php endforeach; ?>
|
|
488
|
-
</select>
|
|
489
|
-
<span class="description">Default role assigned to newly registered users.</span>
|
|
490
|
-
<?php
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
function forgewp_auth_verification_email_subject_render() {
|
|
494
|
-
$value = get_option( 'forgewp_auth_verification_email_subject', '${defaultVerificationEmailSubject.replace(/'/g, "\\'")}' );
|
|
495
|
-
?>
|
|
496
|
-
<input type="text" name="forgewp_auth_verification_email_subject" value="<?php echo esc_attr( $value ); ?>" class="large-text" />
|
|
497
|
-
<?php
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
function forgewp_auth_verification_email_body_render() {
|
|
501
|
-
$value = get_option( 'forgewp_auth_verification_email_body', '${defaultVerificationEmailBody.replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}' );
|
|
502
|
-
?>
|
|
503
|
-
<textarea name="forgewp_auth_verification_email_body" rows="6" class="large-text code"><?php echo esc_textarea( $value ); ?></textarea>
|
|
504
|
-
<p class="description">Use placeholders: <code>{username}</code>, <code>{email}</code>, <code>{verification_url}</code></p>
|
|
505
|
-
<?php
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
function forgewp_auth_password_reset_email_subject_render() {
|
|
509
|
-
$value = get_option( 'forgewp_auth_password_reset_email_subject', '${defaultPasswordResetEmailSubject.replace(/'/g, "\\'")}' );
|
|
510
|
-
?>
|
|
511
|
-
<input type="text" name="forgewp_auth_password_reset_email_subject" value="<?php echo esc_attr( $value ); ?>" class="large-text" />
|
|
512
|
-
<?php
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
function forgewp_auth_password_reset_email_body_render() {
|
|
516
|
-
$value = get_option( 'forgewp_auth_password_reset_email_body', '${defaultPasswordResetEmailBody.replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}' );
|
|
517
|
-
?>
|
|
518
|
-
<textarea name="forgewp_auth_password_reset_email_body" rows="6" class="large-text code"><?php echo esc_textarea( $value ); ?></textarea>
|
|
519
|
-
<p class="description">Use placeholders: <code>{username}</code>, <code>{reset_url}</code></p>
|
|
520
|
-
<?php
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
/**
|
|
524
|
-
* Enforce login field policy globally.
|
|
525
|
-
*/
|
|
526
|
-
add_filter( 'authenticate', 'forgewp_enforce_login_field_policy', 9, 3 );
|
|
527
|
-
function forgewp_enforce_login_field_policy( $user, $username, $password ) {
|
|
528
|
-
if ( empty( $username ) ) {
|
|
529
|
-
return $user;
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
$policy = get_option( 'forgewp_auth_login_field', '${defaultLoginField}' );
|
|
533
|
-
|
|
534
|
-
if ( $policy === 'usernameOnly' && is_email( $username ) ) {
|
|
535
|
-
return new WP_Error( 'invalid_login_field', 'Logins are restricted to usernames only.' );
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
if ( $policy === 'emailOnly' && ! is_email( $username ) ) {
|
|
539
|
-
return new WP_Error( 'invalid_login_field', 'Logins are restricted to email addresses only.' );
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
return $user;
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
/**
|
|
546
|
-
* Intercept mail in local development environment and log to cms/email-logs.json.
|
|
547
|
-
*/
|
|
548
|
-
add_filter( 'pre_wp_mail', 'forgewp_intercept_mail', 10, 2 );
|
|
549
|
-
function forgewp_intercept_mail( $bypass, $mail_data ) {
|
|
550
|
-
$is_dev = false;
|
|
551
|
-
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
|
552
|
-
$is_dev = true;
|
|
553
|
-
}
|
|
554
|
-
if ( defined( 'WP_ENV' ) && WP_ENV === 'development' ) {
|
|
555
|
-
$is_dev = true;
|
|
556
|
-
}
|
|
557
|
-
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
|
|
558
|
-
$host = $_SERVER['HTTP_HOST'];
|
|
559
|
-
if ( $host === 'localhost' || $host === '127.0.0.1' || strpos( $host, '.local' ) !== false ) {
|
|
560
|
-
$is_dev = true;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
if ( ! $is_dev ) {
|
|
564
|
-
return $bypass;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
$cms_dir = get_template_directory() . '/cms';
|
|
568
|
-
if ( ! file_exists( $cms_dir ) ) {
|
|
569
|
-
wp_mkdir_p( $cms_dir );
|
|
570
|
-
}
|
|
571
|
-
$log_file = $cms_dir . '/email-logs.json';
|
|
572
|
-
$emails = array();
|
|
573
|
-
if ( file_exists( $log_file ) ) {
|
|
574
|
-
$existing = json_decode( file_get_contents( $log_file ), true );
|
|
575
|
-
if ( is_array( $existing ) ) {
|
|
576
|
-
$emails = $existing;
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
$verification_url = '';
|
|
581
|
-
if ( preg_match( '/https?:\\/\\/[^\\s]+/i', $mail_data['message'], $matches ) ) {
|
|
582
|
-
$verification_url = $matches[0];
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
$emails[] = array(
|
|
586
|
-
'to' => $mail_data['to'],
|
|
587
|
-
'subject' => $mail_data['subject'],
|
|
588
|
-
'message' => $mail_data['message'],
|
|
589
|
-
'verification_url' => $verification_url,
|
|
590
|
-
'timestamp' => time(),
|
|
591
|
-
);
|
|
592
|
-
|
|
593
|
-
file_put_contents( $log_file, json_encode( $emails, JSON_PRETTY_PRINT ) );
|
|
594
|
-
return true;
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
* Helper to dynamically determine the frontend origin.
|
|
599
|
-
*/
|
|
600
|
-
function forgewp_get_frontend_origin() {
|
|
601
|
-
$origin = '';
|
|
602
|
-
if ( ! empty( $_SERVER['HTTP_ORIGIN'] ) ) {
|
|
603
|
-
$origin = $_SERVER['HTTP_ORIGIN'];
|
|
604
|
-
} elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
|
|
605
|
-
$parts = wp_parse_url( $_SERVER['HTTP_REFERER'] );
|
|
606
|
-
if ( isset( $parts['scheme'] ) && isset( $parts['host'] ) ) {
|
|
607
|
-
$origin = $parts['scheme'] . '://' . $parts['host'];
|
|
608
|
-
if ( isset( $parts['port'] ) ) {
|
|
609
|
-
$origin .= ':' . $parts['port'];
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
if ( empty( $origin ) ) {
|
|
614
|
-
$origin = home_url();
|
|
615
|
-
}
|
|
616
|
-
return rtrim( $origin, '/' );
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
add_action( 'rest_api_init', function () {
|
|
620
|
-
register_rest_route( 'forgewp/v1/auth', '/login', array(
|
|
621
|
-
'methods' => 'POST',
|
|
622
|
-
'callback' => 'forgewp_rest_login',
|
|
623
|
-
'permission_callback' => '__return_true',
|
|
624
|
-
) );
|
|
625
|
-
|
|
626
|
-
register_rest_route( 'forgewp/v1/auth', '/logout', array(
|
|
627
|
-
'methods' => 'POST',
|
|
628
|
-
'callback' => 'forgewp_rest_logout',
|
|
629
|
-
'permission_callback' => '__return_true',
|
|
630
|
-
) );
|
|
631
|
-
|
|
632
|
-
register_rest_route( 'forgewp/v1/auth', '/register', array(
|
|
633
|
-
'methods' => 'POST',
|
|
634
|
-
'callback' => 'forgewp_rest_register',
|
|
635
|
-
'permission_callback' => '__return_true',
|
|
636
|
-
) );
|
|
637
|
-
|
|
638
|
-
register_rest_route( 'forgewp/v1/auth', '/lost-password', array(
|
|
639
|
-
'methods' => 'POST',
|
|
640
|
-
'callback' => 'forgewp_rest_lost_password',
|
|
641
|
-
'permission_callback' => '__return_true',
|
|
642
|
-
) );
|
|
643
|
-
|
|
644
|
-
register_rest_route( 'forgewp/v1/auth', '/reset-password', array(
|
|
645
|
-
'methods' => 'POST',
|
|
646
|
-
'callback' => 'forgewp_rest_reset_password',
|
|
647
|
-
'permission_callback' => '__return_true',
|
|
648
|
-
) );
|
|
649
|
-
|
|
650
|
-
register_rest_route( 'forgewp/v1/auth', '/verify-email', array(
|
|
651
|
-
'methods' => 'POST',
|
|
652
|
-
'callback' => 'forgewp_rest_verify_email',
|
|
653
|
-
'permission_callback' => '__return_true',
|
|
654
|
-
) );
|
|
655
|
-
|
|
656
|
-
register_rest_route( 'forgewp/v1/auth', '/resend-verification', array(
|
|
657
|
-
'methods' => 'POST',
|
|
658
|
-
'callback' => 'forgewp_rest_resend_verification',
|
|
659
|
-
'permission_callback' => '__return_true',
|
|
660
|
-
) );
|
|
661
|
-
|
|
662
|
-
register_rest_route( 'forgewp/v1/auth', '/config', array(
|
|
663
|
-
'methods' => 'GET',
|
|
664
|
-
'callback' => 'forgewp_rest_auth_config',
|
|
665
|
-
'permission_callback' => '__return_true',
|
|
666
|
-
) );
|
|
667
|
-
} );
|
|
668
|
-
|
|
669
|
-
function forgewp_rest_auth_config() {
|
|
670
|
-
return rest_ensure_response( array(
|
|
671
|
-
'loginField' => get_option( 'forgewp_auth_login_field', '${defaultLoginField}' ),
|
|
672
|
-
'emailVerificationEnabled' => get_option( 'forgewp_auth_email_verification_enabled', '${defaultEmailVerificationEnabled}' ) === '1',
|
|
673
|
-
) );
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
function forgewp_rest_login( $request ) {
|
|
677
|
-
$params = $request->get_json_params();
|
|
678
|
-
$username = isset( $params['username'] ) ? sanitize_text_field( $params['username'] ) : '';
|
|
679
|
-
$password = isset( $params['password'] ) ? $params['password'] : '';
|
|
680
|
-
|
|
681
|
-
if ( empty( $username ) || empty( $password ) ) {
|
|
682
|
-
return new WP_Error( 'rest_missing_fields', 'Username and password are required.', array( 'status' => 400 ) );
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
$creds = array(
|
|
686
|
-
'user_login' => $username,
|
|
687
|
-
'user_password' => $password,
|
|
688
|
-
'remember' => true,
|
|
689
|
-
);
|
|
690
|
-
|
|
691
|
-
$user = wp_signon( $creds, is_ssl() );
|
|
692
|
-
|
|
693
|
-
if ( is_wp_error( $user ) ) {
|
|
694
|
-
return new WP_Error( 'rest_forbidden', $user->get_error_message(), array( 'status' => 403 ) );
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
wp_set_current_user( $user->ID );
|
|
698
|
-
|
|
699
|
-
return rest_ensure_response( forgewp_get_current_user_hydration_payload() );
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
function forgewp_rest_logout() {
|
|
703
|
-
wp_logout();
|
|
704
|
-
return rest_ensure_response( array( 'success' => true ) );
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
function forgewp_rest_register( $request ) {
|
|
708
|
-
$registration_enabled = get_option( 'forgewp_auth_registration_enabled', '${defaultRegistrationEnabled}' );
|
|
709
|
-
$users_can_register = get_option( 'users_can_register' );
|
|
710
|
-
if ( $registration_enabled !== '1' || $users_can_register !== '1' ) {
|
|
711
|
-
return new WP_Error( 'rest_registration_disabled', 'User registration is disabled on this site.', array( 'status' => 403 ) );
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
$params = $request->get_json_params();
|
|
715
|
-
$username = isset( $params['username'] ) ? sanitize_text_field( $params['username'] ) : '';
|
|
716
|
-
$email = isset( $params['email'] ) ? sanitize_email( $params['email'] ) : '';
|
|
717
|
-
$password = isset( $params['password'] ) ? $params['password'] : '';
|
|
718
|
-
|
|
719
|
-
if ( empty( $username ) || empty( $email ) || empty( $password ) ) {
|
|
720
|
-
return new WP_Error( 'rest_missing_fields', 'Username, email, and password are required.', array( 'status' => 400 ) );
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
// Reserved usernames validation
|
|
724
|
-
$reserved = array(${reservedUsernamesPhp});
|
|
725
|
-
if ( in_array( strtolower( $username ), $reserved ) ) {
|
|
726
|
-
return new WP_Error( 'rest_invalid_username', 'This username is reserved and cannot be registered.', array( 'status' => 400 ) );
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
if ( username_exists( $username ) ) {
|
|
730
|
-
return new WP_Error( 'rest_username_exists', 'Username already exists.', array( 'status' => 400 ) );
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
if ( email_exists( $email ) ) {
|
|
734
|
-
return new WP_Error( 'rest_email_exists', 'Email address already exists.', array( 'status' => 400 ) );
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
$user_id = wp_create_user( $username, $password, $email );
|
|
738
|
-
|
|
739
|
-
if ( is_wp_error( $user_id ) ) {
|
|
740
|
-
return new WP_Error( 'rest_registration_failed', $user_id->get_error_message(), array( 'status' => 500 ) );
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
$user = new WP_User( $user_id );
|
|
744
|
-
$default_role = get_option( 'forgewp_auth_default_role', '${defaultDefaultRole}' );
|
|
745
|
-
$user->set_role( $default_role );
|
|
746
|
-
|
|
747
|
-
// Save custom profile fields / metadata
|
|
748
|
-
$metadata = isset( $params['metadata'] ) && is_array( $params['metadata'] ) ? $params['metadata'] : array();
|
|
749
|
-
if ( ! empty( $metadata ) ) {
|
|
750
|
-
$existing_keys = get_option( 'forgewp_registered_user_meta_keys', array() );
|
|
751
|
-
if ( ! is_array( $existing_keys ) ) {
|
|
752
|
-
$existing_keys = array();
|
|
753
|
-
}
|
|
754
|
-
$new_keys = array();
|
|
755
|
-
foreach ( array_keys( $metadata ) as $key ) {
|
|
756
|
-
$clean_key = sanitize_key( $key );
|
|
757
|
-
if ( ! empty( $clean_key ) ) {
|
|
758
|
-
$new_keys[] = $clean_key;
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
$updated_keys = array_unique( array_merge( $existing_keys, $new_keys ) );
|
|
762
|
-
if ( $updated_keys !== $existing_keys ) {
|
|
763
|
-
update_option( 'forgewp_registered_user_meta_keys', $updated_keys );
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
|
|
767
|
-
foreach ( $metadata as $meta_key => $meta_val ) {
|
|
768
|
-
$clean_key = sanitize_key( $meta_key );
|
|
769
|
-
if ( ! empty( $clean_key ) ) {
|
|
770
|
-
if ( is_array( $meta_val ) ) {
|
|
771
|
-
$clean_val = map_deep( $meta_val, 'sanitize_text_field' );
|
|
772
|
-
} else {
|
|
773
|
-
$clean_val = sanitize_text_field( $meta_val );
|
|
774
|
-
}
|
|
775
|
-
update_user_meta( $user_id, $clean_key, $clean_val );
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
$verification_enabled = get_option( 'forgewp_auth_email_verification_enabled', '${defaultEmailVerificationEnabled}' ) === '1';
|
|
780
|
-
|
|
781
|
-
if ( $verification_enabled ) {
|
|
782
|
-
update_user_meta( $user_id, 'forgewp_email_verified', '0' );
|
|
783
|
-
|
|
784
|
-
$token = wp_generate_password( 32, false );
|
|
785
|
-
set_transient( 'forgewp_verify_email_' . $token, $user_id, DAY_IN_SECONDS );
|
|
786
|
-
|
|
787
|
-
$origin = forgewp_get_frontend_origin();
|
|
788
|
-
$verification_url = $origin . '/verify-email?userId=' . $user_id . '&token=' . $token;
|
|
789
|
-
|
|
790
|
-
$subject = get_option( 'forgewp_auth_verification_email_subject', '${defaultVerificationEmailSubject.replace(/'/g, "\\'")}' );
|
|
791
|
-
$body = get_option( 'forgewp_auth_verification_email_body', '${defaultVerificationEmailBody.replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}' );
|
|
792
|
-
|
|
793
|
-
$body = str_replace(
|
|
794
|
-
array( '{username}', '{email}', '{verification_url}' ),
|
|
795
|
-
array( $username, $email, $verification_url ),
|
|
796
|
-
$body
|
|
797
|
-
);
|
|
798
|
-
|
|
799
|
-
wp_mail( $email, $subject, $body );
|
|
800
|
-
|
|
801
|
-
return rest_ensure_response( array(
|
|
802
|
-
'success' => true,
|
|
803
|
-
'requiresVerification' => true,
|
|
804
|
-
) );
|
|
805
|
-
} else {
|
|
806
|
-
update_user_meta( $user_id, 'forgewp_email_verified', '1' );
|
|
807
|
-
|
|
808
|
-
$auto_login = '${defaultAutoLoginAfterSignup}' === '1';
|
|
809
|
-
if ( $auto_login ) {
|
|
810
|
-
$creds = array(
|
|
811
|
-
'user_login' => $username,
|
|
812
|
-
'user_password' => $password,
|
|
813
|
-
'remember' => true,
|
|
814
|
-
);
|
|
815
|
-
$signed_in_user = wp_signon( $creds, is_ssl() );
|
|
816
|
-
if ( ! is_wp_error( $signed_in_user ) ) {
|
|
817
|
-
wp_set_current_user( $signed_in_user->ID );
|
|
818
|
-
}
|
|
819
|
-
return rest_ensure_response( forgewp_get_current_user_hydration_payload() );
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
return rest_ensure_response( array(
|
|
823
|
-
'success' => true,
|
|
824
|
-
'requiresVerification' => false,
|
|
825
|
-
) );
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
function forgewp_rest_verify_email( $request ) {
|
|
830
|
-
$params = $request->get_json_params();
|
|
831
|
-
$user_id = isset( $params['userId'] ) ? intval( $params['userId'] ) : 0;
|
|
832
|
-
$token = isset( $params['token'] ) ? sanitize_text_field( $params['token'] ) : '';
|
|
833
|
-
|
|
834
|
-
if ( ! $user_id || ! $token ) {
|
|
835
|
-
return new WP_Error( 'rest_missing_fields', 'User ID and token are required.', array( 'status' => 400 ) );
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
$transient_name = 'forgewp_verify_email_' . $token;
|
|
839
|
-
$stored_user_id = get_transient( $transient_name );
|
|
840
|
-
|
|
841
|
-
if ( ! $stored_user_id || intval( $stored_user_id ) !== $user_id ) {
|
|
842
|
-
return new WP_Error( 'rest_invalid_token', 'Invalid or expired verification token.', array( 'status' => 400 ) );
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
update_user_meta( $user_id, 'forgewp_email_verified', '1' );
|
|
846
|
-
delete_transient( $transient_name );
|
|
847
|
-
|
|
848
|
-
$user = get_user_by( 'id', $user_id );
|
|
849
|
-
if ( $user ) {
|
|
850
|
-
wp_set_current_user( $user_id );
|
|
851
|
-
wp_set_auth_cookie( $user_id, true );
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
return rest_ensure_response( array(
|
|
855
|
-
'success' => true,
|
|
856
|
-
'user' => forgewp_get_current_user_hydration_payload(),
|
|
857
|
-
) );
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
function forgewp_rest_resend_verification( $request ) {
|
|
861
|
-
$params = $request->get_json_params();
|
|
862
|
-
$login_input = isset( $params['email'] ) ? sanitize_text_field( $params['email'] ) : '';
|
|
863
|
-
$login_field = get_option( 'forgewp_auth_login_field', '${defaultLoginField}' );
|
|
864
|
-
|
|
865
|
-
if ( empty( $login_input ) ) {
|
|
866
|
-
$required_msg = 'Username or email is required.';
|
|
867
|
-
if ( $login_field === 'emailOnly' ) {
|
|
868
|
-
$required_msg = 'Email is required.';
|
|
869
|
-
} elseif ( $login_field === 'usernameOnly' ) {
|
|
870
|
-
$required_msg = 'Username is required.';
|
|
871
|
-
}
|
|
872
|
-
return new WP_Error( 'rest_missing_fields', $required_msg, array( 'status' => 400 ) );
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
$user = null;
|
|
876
|
-
if ( is_email( $login_input ) ) {
|
|
877
|
-
$user = get_user_by( 'email', $login_input );
|
|
878
|
-
}
|
|
879
|
-
if ( ! $user ) {
|
|
880
|
-
$user = get_user_by( 'login', $login_input );
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
if ( ! $user ) {
|
|
884
|
-
$error_msg = 'No account found with this email address or username.';
|
|
885
|
-
if ( $login_field === 'emailOnly' ) {
|
|
886
|
-
$error_msg = 'No account found with this email address.';
|
|
887
|
-
} elseif ( $login_field === 'usernameOnly' ) {
|
|
888
|
-
$error_msg = 'No account found with this username.';
|
|
889
|
-
}
|
|
890
|
-
return new WP_Error( 'rest_invalid_user', $error_msg, array( 'status' => 404 ) );
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
$email = $user->user_email;
|
|
894
|
-
|
|
895
|
-
$verified = get_user_meta( $user->ID, 'forgewp_email_verified', true );
|
|
896
|
-
if ( $verified === '1' ) {
|
|
897
|
-
return new WP_Error( 'rest_already_verified', 'This email address is already verified.', array( 'status' => 400 ) );
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
$token = wp_generate_password( 32, false );
|
|
901
|
-
set_transient( 'forgewp_verify_email_' . $token, $user->ID, DAY_IN_SECONDS );
|
|
902
|
-
|
|
903
|
-
$origin = forgewp_get_frontend_origin();
|
|
904
|
-
$verification_url = $origin . '/verify-email?userId=' . $user->ID . '&token=' . $token;
|
|
905
|
-
|
|
906
|
-
$subject = get_option( 'forgewp_auth_verification_email_subject', '${defaultVerificationEmailSubject.replace(/'/g, "\\'")}' );
|
|
907
|
-
$body = get_option( 'forgewp_auth_verification_email_body', '${defaultVerificationEmailBody.replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}' );
|
|
908
|
-
|
|
909
|
-
$body = str_replace(
|
|
910
|
-
array( '{username}', '{email}', '{verification_url}' ),
|
|
911
|
-
array( $user->user_login, $email, $verification_url ),
|
|
912
|
-
$body
|
|
913
|
-
);
|
|
914
|
-
|
|
915
|
-
wp_mail( $email, $subject, $body );
|
|
916
|
-
|
|
917
|
-
return rest_ensure_response( array(
|
|
918
|
-
'success' => true
|
|
919
|
-
) );
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
function forgewp_rest_lost_password( $request ) {
|
|
923
|
-
$params = $request->get_json_params();
|
|
924
|
-
$user_login = isset( $params['user_login'] ) ? sanitize_text_field( $params['user_login'] ) : '';
|
|
925
|
-
|
|
926
|
-
if ( empty( $user_login ) ) {
|
|
927
|
-
return new WP_Error( 'rest_missing_fields', 'Email or username is required.', array( 'status' => 400 ) );
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
if ( ! function_exists( 'retrieve_password' ) ) {
|
|
931
|
-
require_once ABSPATH . 'wp-login.php';
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
$errors = retrieve_password( $user_login );
|
|
935
|
-
|
|
936
|
-
if ( is_wp_error( $errors ) ) {
|
|
937
|
-
return new WP_Error( 'rest_recovery_failed', $errors->get_error_message(), array( 'status' => 400 ) );
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
return rest_ensure_response( array( 'success' => true ) );
|
|
941
|
-
}
|
|
942
|
-
|
|
943
|
-
function forgewp_rest_reset_password( $request ) {
|
|
944
|
-
$params = $request->get_json_params();
|
|
945
|
-
$user_login = isset( $params['login'] ) ? sanitize_text_field( $params['login'] ) : '';
|
|
946
|
-
$key = isset( $params['key'] ) ? sanitize_text_field( $params['key'] ) : '';
|
|
947
|
-
$password = isset( $params['password'] ) ? $params['password'] : '';
|
|
948
|
-
|
|
949
|
-
if ( empty( $user_login ) || empty( $key ) || empty( $password ) ) {
|
|
950
|
-
return new WP_Error( 'rest_missing_fields', 'Username, key, and password are required.', array( 'status' => 400 ) );
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
if ( ! function_exists( 'check_password_reset_key' ) ) {
|
|
954
|
-
require_once ABSPATH . 'wp-login.php';
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
$user = check_password_reset_key( $key, $user_login );
|
|
958
|
-
|
|
959
|
-
if ( is_wp_error( $user ) ) {
|
|
960
|
-
return new WP_Error( 'rest_invalid_key', 'The password reset link is invalid or has expired.', array( 'status' => 400 ) );
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
reset_password( $user, $password );
|
|
964
|
-
|
|
965
|
-
return rest_ensure_response( array( 'success' => true ) );
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
/**
|
|
969
|
-
* Intercept retrieve password mail subject and body to support custom template and headless redirect.
|
|
970
|
-
*/
|
|
971
|
-
add_filter( 'retrieve_password_title', 'forgewp_retrieve_password_title', 10, 3 );
|
|
972
|
-
function forgewp_retrieve_password_title( $title, $user_login, $user_data ) {
|
|
973
|
-
$custom_subject = get_option( 'forgewp_auth_password_reset_email_subject', '${defaultPasswordResetEmailSubject.replace(/'/g, "\\'")}' );
|
|
974
|
-
if ( ! empty( $custom_subject ) ) {
|
|
975
|
-
return $custom_subject;
|
|
976
|
-
}
|
|
977
|
-
return $title;
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
add_filter( 'retrieve_password_message', 'forgewp_retrieve_password_message', 10, 4 );
|
|
981
|
-
function forgewp_retrieve_password_message( $message, $key, $user_login, $user_data ) {
|
|
982
|
-
$origin = forgewp_get_frontend_origin();
|
|
983
|
-
$reset_url = $origin . '/reset-password?key=' . $key . '&login=' . rawurlencode( $user_login );
|
|
984
|
-
|
|
985
|
-
$custom_body = get_option( 'forgewp_auth_password_reset_email_body', '${defaultPasswordResetEmailBody.replace(/\r?\n/g, '\\n').replace(/'/g, "\\'")}' );
|
|
986
|
-
if ( ! empty( $custom_body ) ) {
|
|
987
|
-
$body = str_replace(
|
|
988
|
-
array( '{username}', '{reset_url}' ),
|
|
989
|
-
array( $user_login, $reset_url ),
|
|
990
|
-
$custom_body
|
|
991
|
-
);
|
|
992
|
-
return $body;
|
|
993
|
-
}
|
|
994
|
-
|
|
995
|
-
$message = "Hello!\\n\\nYou asked us to reset your password for your account: " . $user_login . "\\n\\nIf this was a mistake, just ignore this email.\\n\\nTo reset your password, visit the following link:\\n\\n" . $reset_url . "\\n";
|
|
996
|
-
return $message;
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
function forgewp_auth_head_hydration() {
|
|
1000
|
-
$payload = forgewp_get_current_user_hydration_payload();
|
|
1001
|
-
$login_field = get_option( 'forgewp_auth_login_field', '${defaultLoginField}' );
|
|
1002
|
-
$verification_enabled = get_option( 'forgewp_auth_email_verification_enabled', '${defaultEmailVerificationEnabled}' ) === '1';
|
|
1003
|
-
|
|
1004
|
-
$session_data = array(
|
|
1005
|
-
'loggedIn' => ! empty( $payload ),
|
|
1006
|
-
'user' => $payload,
|
|
1007
|
-
'loginField' => $login_field,
|
|
1008
|
-
'emailVerificationEnabled' => $verification_enabled,
|
|
1009
|
-
);
|
|
1010
|
-
?>
|
|
1011
|
-
<script type="application/json" id="forgewp-session">
|
|
1012
|
-
<?php echo wp_json_encode($session_data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE); ?>
|
|
1013
|
-
</script>
|
|
1014
|
-
<script id="forgewp-auth-hydration">
|
|
1015
|
-
if (!window.forgeWpHydration) {
|
|
1016
|
-
window.forgeWpHydration = {};
|
|
1017
|
-
}
|
|
1018
|
-
window.forgeWpHydration.currentUser = <?php echo wp_json_encode($payload, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE); ?>;
|
|
1019
|
-
window.forgeWpHydration.loginField = <?php echo wp_json_encode($login_field); ?>;
|
|
1020
|
-
window.forgeWpHydration.emailVerificationEnabled = <?php echo wp_json_encode($verification_enabled); ?>;
|
|
1021
|
-
</script>
|
|
1022
|
-
<?php
|
|
1023
|
-
}
|
|
1024
|
-
add_action('wp_head', 'forgewp_auth_head_hydration', 1);
|
|
1025
|
-
`;
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
|
-
const css = assets.cssFile.replace(/^assets\//, '');
|
|
1029
|
-
|
|
1030
|
-
let seoPluginsPhp = '';
|
|
1031
|
-
if (config.seo && config.seo.plugins) {
|
|
1032
|
-
const { yoast, rankMath } = config.seo.plugins;
|
|
1033
|
-
if (yoast) {
|
|
1034
|
-
seoPluginsPhp += `
|
|
1035
|
-
/**
|
|
1036
|
-
* Automatically integrate ForgeWP enqueued schemas with Yoast SEO Schema Graph.
|
|
1037
|
-
*/
|
|
1038
|
-
add_filter('wpseo_schema_graph', function($graph) {
|
|
1039
|
-
$target = forgewp_resolve_head_target();
|
|
1040
|
-
if (file_exists($target)) {
|
|
1041
|
-
ob_start();
|
|
1042
|
-
include $target;
|
|
1043
|
-
$content = ob_get_clean();
|
|
1044
|
-
if (preg_match_all('/<script type="application\\/ld\\+json">(.*?)<\\/script>/is', $content, $matches)) {
|
|
1045
|
-
foreach ($matches[1] as $json_str) {
|
|
1046
|
-
$decoded = json_decode(trim($json_str), true);
|
|
1047
|
-
if (is_array($decoded) && is_array($graph)) {
|
|
1048
|
-
$graph[] = $decoded;
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
}
|
|
1053
|
-
return $graph;
|
|
1054
|
-
});
|
|
1055
|
-
`;
|
|
1056
|
-
}
|
|
1057
|
-
if (rankMath) {
|
|
1058
|
-
seoPluginsPhp += `
|
|
1059
|
-
/**
|
|
1060
|
-
* Automatically integrate ForgeWP enqueued schemas with RankMath JSON-LD.
|
|
1061
|
-
*/
|
|
1062
|
-
add_filter('rank_math/json_ld', function($data, $jsonld) {
|
|
1063
|
-
$target = forgewp_resolve_head_target();
|
|
1064
|
-
if (file_exists($target)) {
|
|
1065
|
-
ob_start();
|
|
1066
|
-
include $target;
|
|
1067
|
-
$content = ob_get_clean();
|
|
1068
|
-
if (preg_match_all('/<script type="application\\/ld\\+json">(.*?)<\\/script>/is', $content, $matches)) {
|
|
1069
|
-
foreach ($matches[1] as $json_str) {
|
|
1070
|
-
$decoded = json_decode(trim($json_str), true);
|
|
1071
|
-
if (is_array($decoded) && is_array($data)) {
|
|
1072
|
-
$data[] = $decoded;
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
return $data;
|
|
1078
|
-
}, 99, 2);
|
|
1079
|
-
`;
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
|
|
1083
|
-
// Built-in Favicon Support
|
|
1084
|
-
let faviconPhp = '';
|
|
1085
|
-
if (config.favicon) {
|
|
1086
|
-
const faviconPath = config.favicon.replace(/^\//, ''); // Clean leading slash
|
|
1087
|
-
faviconPhp = `
|
|
1088
|
-
/**
|
|
1089
|
-
* Enqueue Favicon dynamically in WordPress head (generated from wp.config.ts).
|
|
1090
|
-
*/
|
|
1091
|
-
function forgewp_add_favicon() {
|
|
1092
|
-
$uri = get_template_directory_uri() . '/${faviconPath}';
|
|
1093
|
-
$ext = strtolower(pathinfo($uri, PATHINFO_EXTENSION));
|
|
1094
|
-
$type = 'image/x-icon';
|
|
1095
|
-
if ($ext === 'svg') {
|
|
1096
|
-
$type = 'image/svg+xml';
|
|
1097
|
-
} elseif ($ext === 'png') {
|
|
1098
|
-
$type = 'image/png';
|
|
1099
|
-
} elseif ($ext === 'gif') {
|
|
1100
|
-
$type = 'image/gif';
|
|
1101
|
-
} elseif ($ext === 'jpg' || $ext === 'jpeg') {
|
|
1102
|
-
$type = 'image/jpeg';
|
|
1103
|
-
}
|
|
1104
|
-
echo '<link rel="icon" type="' . esc_attr($type) . '" href="' . esc_url($uri) . '">';
|
|
1105
|
-
}
|
|
1106
|
-
add_action('wp_head', 'forgewp_add_favicon');
|
|
1107
|
-
`;
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
// Built-in XML Segmented Sitemaps
|
|
1111
|
-
let sitemapsPhp = '';
|
|
1112
|
-
if (config.seo && config.seo.sitemaps) {
|
|
1113
|
-
const { postTypes = [], taxonomies = [] } = config.seo.sitemaps;
|
|
1114
|
-
|
|
1115
|
-
let postTypesLines = '';
|
|
1116
|
-
for (const pt of postTypes) {
|
|
1117
|
-
postTypesLines += ` $post_types['${pt}'] = get_post_type_object('${pt}');\n`;
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
let taxonomiesLines = '';
|
|
1121
|
-
for (const tax of taxonomies) {
|
|
1122
|
-
taxonomiesLines += ` $taxonomies['${tax}'] = get_taxonomy('${tax}');\n`;
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
sitemapsPhp = `
|
|
1126
|
-
/**
|
|
1127
|
-
* Custom XML Segmented Sitemaps Configuration (generated from wp.config.ts).
|
|
1128
|
-
*/
|
|
1129
|
-
function forgewp_register_segmented_xml_sitemaps() {
|
|
1130
|
-
if (!function_exists('wp_sitemaps_get_server')) {
|
|
1131
|
-
return;
|
|
1132
|
-
}
|
|
1133
|
-
if (defined('WPSEO_VERSION') || class_exists('RankMath') || class_exists('All_in_One_SEO_Pack') || defined('AIOSEO_VERSION')) {
|
|
1134
|
-
return;
|
|
1135
|
-
}
|
|
1136
|
-
|
|
1137
|
-
add_filter('wp_sitemaps_post_types', function($post_types) {
|
|
1138
|
-
${postTypesLines} return $post_types;
|
|
1139
|
-
});
|
|
1140
|
-
|
|
1141
|
-
add_filter('wp_sitemaps_taxonomies', function($taxonomies) {
|
|
1142
|
-
${taxonomiesLines} return $taxonomies;
|
|
1143
|
-
});
|
|
1144
|
-
}
|
|
1145
|
-
add_action('init', 'forgewp_register_segmented_xml_sitemaps', 99);
|
|
1146
|
-
`;
|
|
1147
|
-
}
|
|
1148
|
-
const version = config.version.replace(/'/g, "\\'");
|
|
1149
|
-
const googleFonts = config.settings?.typography?.googleFonts || [];
|
|
1150
|
-
|
|
1151
|
-
// Load site-settings.json to discover social media options dynamically
|
|
1152
|
-
let socialKeys = ['facebook', 'instagram', 'twitter', 'youtube', 'tiktok', 'linkedin', 'pinterest']; // default fallbacks
|
|
1153
|
-
try {
|
|
1154
|
-
const siteSettingsPath = path.join(themeRoot, 'cms', 'site-settings.json');
|
|
1155
|
-
if (existsSync(siteSettingsPath)) {
|
|
1156
|
-
const siteSettings = JSON.parse(readFileSync(siteSettingsPath, 'utf8'));
|
|
1157
|
-
const optionsKeys = Object.keys(siteSettings.options || {});
|
|
1158
|
-
const discoveredSocials = optionsKeys
|
|
1159
|
-
.filter(k => k.startsWith('social_'))
|
|
1160
|
-
.map(k => k.replace(/^social_/, ''));
|
|
1161
|
-
if (discoveredSocials.length > 0) {
|
|
1162
|
-
socialKeys = Array.from(new Set([...socialKeys, ...discoveredSocials]));
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
} catch (e) {
|
|
1166
|
-
// fallback to defaults if read fails
|
|
1167
|
-
}
|
|
1168
|
-
|
|
1169
|
-
let socialSettingsPhp = '';
|
|
1170
|
-
for (const key of socialKeys) {
|
|
1171
|
-
const optionName = `social_${key}`;
|
|
1172
|
-
const capitalizedLabel = key.split('_').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(' ');
|
|
1173
|
-
socialSettingsPhp += `
|
|
1174
|
-
// \${optionName}
|
|
1175
|
-
\$wp_customize->add_setting(
|
|
1176
|
-
'${optionName}',
|
|
1177
|
-
array(
|
|
1178
|
-
'type' => 'option',
|
|
1179
|
-
'default' => '',
|
|
1180
|
-
'sanitize_callback' => 'esc_url_raw',
|
|
1181
|
-
)
|
|
1182
|
-
);
|
|
1183
|
-
\$wp_customize->add_control(
|
|
1184
|
-
'${optionName}',
|
|
1185
|
-
array(
|
|
1186
|
-
'label' => __('${capitalizedLabel} URL', '${config.textDomain}'),
|
|
1187
|
-
'section' => 'forgewp_social_section',
|
|
1188
|
-
'type' => 'url',
|
|
1189
|
-
)
|
|
1190
|
-
);`;
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
|
-
const uniqueMetaKeys = new Set();
|
|
1194
|
-
const metaKeyTypes = {};
|
|
1195
|
-
const metaKeysToSkip = new Set();
|
|
1196
|
-
|
|
1197
|
-
for (const [templateSlug, schema] of Object.entries(schemas)) {
|
|
1198
|
-
for (const [key, field] of Object.entries(schema)) {
|
|
1199
|
-
uniqueMetaKeys.add(key);
|
|
1200
|
-
const typeMap = {
|
|
1201
|
-
text: 'string',
|
|
1202
|
-
richText: 'string',
|
|
1203
|
-
image: 'string',
|
|
1204
|
-
boolean: 'boolean',
|
|
1205
|
-
repeater: 'string',
|
|
1206
|
-
};
|
|
1207
|
-
metaKeyTypes[key] = typeMap[field.type] || 'string';
|
|
1208
|
-
if (field.skipRegisterMeta === true || field.customType === 'relationship') {
|
|
1209
|
-
metaKeysToSkip.add(key);
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
const uniqueRichTextKeys = [];
|
|
1215
|
-
for (const [templateSlug, schema] of Object.entries(schemas)) {
|
|
1216
|
-
for (const [key, field] of Object.entries(schema)) {
|
|
1217
|
-
if (field.type === 'richText' && !uniqueRichTextKeys.includes(key)) {
|
|
1218
|
-
uniqueRichTextKeys.push(key);
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
}
|
|
1222
|
-
|
|
1223
|
-
let postTypes = [];
|
|
1224
|
-
let registeredTaxonomies = [];
|
|
1225
|
-
const polylangMetaStrings = [];
|
|
1226
|
-
for (const [templateSlug, schema] of Object.entries(schemas)) {
|
|
1227
|
-
for (const [key, field] of Object.entries(schema)) {
|
|
1228
|
-
if ((field.type === 'text' || field.type === 'richText') && field.default) {
|
|
1229
|
-
polylangMetaStrings.push(field.default);
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
|
-
const uniquePolylangStrings = Array.from(new Set([
|
|
1235
|
-
...i18nKeys,
|
|
1236
|
-
...polylangMetaStrings
|
|
1237
|
-
]));
|
|
1238
|
-
|
|
1239
|
-
const i18nKeysPhp = uniquePolylangStrings
|
|
1240
|
-
.map((k) => ` '${k.replace(/'/g, "\\'")}',`)
|
|
1241
|
-
.join('\n');
|
|
1242
|
-
const wpBlockStylesLine = config.presets?.wordpressCoreStyles === false
|
|
1243
|
-
? ''
|
|
1244
|
-
: " add_theme_support('wp-block-styles');\n";
|
|
1245
|
-
|
|
1246
|
-
let cptRegistration = '';
|
|
1247
|
-
try {
|
|
1248
|
-
const mockDataPath = path.join(themeRoot, 'cms', 'mock-data.json');
|
|
1249
|
-
if (existsSync(mockDataPath)) {
|
|
1250
|
-
const mockData = JSON.parse(readFileSync(mockDataPath, 'utf8'));
|
|
1251
|
-
postTypes = Object.keys(mockData).filter(
|
|
1252
|
-
(k) =>
|
|
1253
|
-
k !== 'posts' &&
|
|
1254
|
-
k !== 'pages' &&
|
|
1255
|
-
k !== 'post' &&
|
|
1256
|
-
k !== 'page' &&
|
|
1257
|
-
k !== 'menus' &&
|
|
1258
|
-
k !== 'attachment' &&
|
|
1259
|
-
!k.startsWith('_'), // Underscore-prefixed keys are taxonomy definitions, not CPTs
|
|
1260
|
-
);
|
|
1261
|
-
if (postTypes.length > 0) {
|
|
1262
|
-
// Dynamic Taxonomy Scan from post _terms
|
|
1263
|
-
const taxonomiesSet = new Set();
|
|
1264
|
-
const postTypeTaxonomies = {}; // Map postType -> Set of taxonomies
|
|
1265
|
-
const postTypeNativeTaxonomies = {}; // Map postType -> Set of native taxonomies
|
|
1266
|
-
|
|
1267
|
-
for (const pt of postTypes) {
|
|
1268
|
-
if (pt.length > 20) {
|
|
1269
|
-
throw new Error(
|
|
1270
|
-
`Custom Post Type "${pt}" inferred from mock-data.json exceeds the WordPress limit of 20 characters. Please shorten the key.`
|
|
1271
|
-
);
|
|
1272
|
-
}
|
|
1273
|
-
postTypeTaxonomies[pt] = new Set();
|
|
1274
|
-
postTypeNativeTaxonomies[pt] = new Set();
|
|
1275
|
-
const items = mockData[pt];
|
|
1276
|
-
if (Array.isArray(items)) {
|
|
1277
|
-
for (const item of items) {
|
|
1278
|
-
if (item._terms && typeof item._terms === 'object') {
|
|
1279
|
-
for (const tax of Object.keys(item._terms)) {
|
|
1280
|
-
if (tax !== 'category' && tax !== 'post_tag') {
|
|
1281
|
-
taxonomiesSet.add(tax);
|
|
1282
|
-
postTypeTaxonomies[pt].add(tax);
|
|
1283
|
-
} else {
|
|
1284
|
-
postTypeNativeTaxonomies[pt].add(tax);
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
}
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
|
|
1292
|
-
registeredTaxonomies = Array.from(taxonomiesSet);
|
|
1293
|
-
|
|
1294
|
-
cptRegistration = `
|
|
1295
|
-
/**
|
|
1296
|
-
* Register dynamic Custom Post Types inferred from local mock data.
|
|
1297
|
-
*/
|
|
1298
|
-
function forgewp_register_custom_post_types() {
|
|
1299
|
-
${postTypes
|
|
1300
|
-
.map(
|
|
1301
|
-
(pt) => {
|
|
1302
|
-
let singular = pt.split(/[_-]/).map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(' ');
|
|
1303
|
-
let plural = singular + 's';
|
|
1304
|
-
const ptConfig = config.postTypes?.[pt];
|
|
1305
|
-
if (ptConfig?.labels?.singular) {
|
|
1306
|
-
singular = ptConfig.labels.singular;
|
|
1307
|
-
}
|
|
1308
|
-
if (ptConfig?.labels?.plural) {
|
|
1309
|
-
plural = ptConfig.labels.plural;
|
|
1310
|
-
}
|
|
1311
|
-
const nativeTaxes = Array.from(postTypeNativeTaxonomies[pt] || []);
|
|
1312
|
-
const taxonomiesLine = nativeTaxes.length > 0
|
|
1313
|
-
? `\n 'taxonomies' => array('${nativeTaxes.join("', '")}'),`
|
|
1314
|
-
: '';
|
|
1315
|
-
const nativeTaxesCalls = nativeTaxes.map(tax => `\n register_taxonomy_for_object_type('${tax}', '${pt}');`).join('');
|
|
1316
|
-
return ` register_post_type('${pt}', array(
|
|
1317
|
-
'labels' => array(
|
|
1318
|
-
'name' => __('${plural}', '${config.textDomain}'),
|
|
1319
|
-
'singular_name' => __('${singular}', '${config.textDomain}'),
|
|
1320
|
-
'menu_name' => __('${plural}', '${config.textDomain}'),
|
|
1321
|
-
'name_admin_bar' => __('${singular}', '${config.textDomain}'),
|
|
1322
|
-
'add_new' => __('Add New', '${config.textDomain}'),
|
|
1323
|
-
'add_new_item' => __('Add New ${singular}', '${config.textDomain}'),
|
|
1324
|
-
'new_item' => __('New ${singular}', '${config.textDomain}'),
|
|
1325
|
-
'edit_item' => __('Edit ${singular}', '${config.textDomain}'),
|
|
1326
|
-
'view_item' => __('View ${singular}', '${config.textDomain}'),
|
|
1327
|
-
'all_items' => __('All ${plural}', '${config.textDomain}'),
|
|
1328
|
-
'search_items' => __('Search ${plural}', '${config.textDomain}'),
|
|
1329
|
-
'not_found' => __('No ${plural} found.', '${config.textDomain}'),
|
|
1330
|
-
),
|
|
1331
|
-
'public' => true,
|
|
1332
|
-
'has_archive' => true,
|
|
1333
|
-
'show_in_rest'=> true,${taxonomiesLine}
|
|
1334
|
-
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'excerpt'),
|
|
1335
|
-
'menu_icon' => 'dashicons-admin-post',
|
|
1336
|
-
));${nativeTaxesCalls}`;
|
|
1337
|
-
}
|
|
1338
|
-
)
|
|
1339
|
-
.join('\n')}
|
|
1340
|
-
|
|
1341
|
-
${registeredTaxonomies
|
|
1342
|
-
.map((tax) => {
|
|
1343
|
-
const associatedPostTypes = postTypes.filter((pt) => postTypeTaxonomies[pt].has(tax));
|
|
1344
|
-
const taxLabel = tax.split('_').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(' ');
|
|
1345
|
-
return ` register_taxonomy('${tax}', array('${associatedPostTypes.join("', '")}'), array(
|
|
1346
|
-
'labels' => array(
|
|
1347
|
-
'name' => '${taxLabel}s',
|
|
1348
|
-
'singular_name' => '${taxLabel}',
|
|
1349
|
-
'search_items' => 'Search ${taxLabel}s',
|
|
1350
|
-
'all_items' => 'All ${taxLabel}s',
|
|
1351
|
-
'parent_item' => 'Parent ${taxLabel}',
|
|
1352
|
-
'parent_item_colon' => 'Parent ${taxLabel}:',
|
|
1353
|
-
'edit_item' => 'Edit ${taxLabel}',
|
|
1354
|
-
'update_item' => 'Update ${taxLabel}',
|
|
1355
|
-
'add_new_item' => 'Add New ${taxLabel}',
|
|
1356
|
-
'new_item_name' => 'New ${taxLabel} Name',
|
|
1357
|
-
'menu_name' => '${taxLabel}s',
|
|
1358
|
-
),
|
|
1359
|
-
'hierarchical' => true,
|
|
1360
|
-
'public' => true,
|
|
1361
|
-
'show_ui' => true,
|
|
1362
|
-
'show_admin_column' => true,
|
|
1363
|
-
'query_var' => true,
|
|
1364
|
-
'show_in_rest' => true,
|
|
1365
|
-
));`;
|
|
1366
|
-
})
|
|
1367
|
-
.join('\n\n')}
|
|
1368
|
-
}
|
|
1369
|
-
add_action('init', 'forgewp_register_custom_post_types');
|
|
1370
|
-
${(() => {
|
|
1371
|
-
const translatablePtKeys = Object.entries(config.postTypes || {})
|
|
1372
|
-
.filter(([_, ptc]) => ptc.translatable)
|
|
1373
|
-
.map(([key]) => key);
|
|
1374
|
-
let php = translatablePtKeys.length > 0
|
|
1375
|
-
? `\n// Automatically register custom post types for Polylang translation support
|
|
1376
|
-
add_filter('pll_get_post_types', function($post_types, $is_settings) {
|
|
1377
|
-
${translatablePtKeys.map((pt) => ` $post_types['${pt}'] = '${pt}';`).join('\n')}
|
|
1378
|
-
return $post_types;
|
|
1379
|
-
}, 10, 2);`
|
|
1380
|
-
: '';
|
|
1381
|
-
|
|
1382
|
-
if (translatablePtKeys.length > 0) {
|
|
1383
|
-
php += `\n\n// Add REST API language filtering support for custom post types under Polylang
|
|
1384
|
-
function forgewp_rest_query_language_filter($args, $request) {
|
|
1385
|
-
$lang = $request->get_param('lang');
|
|
1386
|
-
if (!empty($lang)) {
|
|
1387
|
-
$args['lang'] = sanitize_text_field($lang);
|
|
1388
|
-
} elseif (function_exists('pll_current_language')) {
|
|
1389
|
-
$args['lang'] = pll_current_language();
|
|
1390
|
-
}
|
|
1391
|
-
return $args;
|
|
1392
|
-
}
|
|
1393
|
-
`;
|
|
1394
|
-
translatablePtKeys.forEach((pt) => {
|
|
1395
|
-
php += `add_filter('rest_${pt}_query', 'forgewp_rest_query_language_filter', 10, 2);\n`;
|
|
1396
|
-
});
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
if (registeredTaxonomies.length > 0) {
|
|
1400
|
-
php += `\n\n// Automatically register custom taxonomies for Polylang translation support
|
|
1401
|
-
add_filter('pll_get_taxonomies', function($taxonomies, $is_settings) {
|
|
1402
|
-
${registeredTaxonomies.map((tax) => ` $taxonomies['${tax}'] = '${tax}';`).join('\n')}
|
|
1403
|
-
return $taxonomies;
|
|
1404
|
-
}, 10, 2);`;
|
|
1405
|
-
|
|
1406
|
-
const taxArrayStr = registeredTaxonomies.map(tax => `'${tax}'`).join(', ');
|
|
1407
|
-
php += `\n\n// Automatically expose custom taxonomy metadata in WordPress REST API
|
|
1408
|
-
add_action('rest_api_init', function() {
|
|
1409
|
-
$taxonomies = array(${taxArrayStr});
|
|
1410
|
-
foreach ($taxonomies as $tax) {
|
|
1411
|
-
register_rest_field($tax, 'meta', array(
|
|
1412
|
-
'get_callback' => function($term) use ($tax) {
|
|
1413
|
-
$term_id = $term['id'];
|
|
1414
|
-
|
|
1415
|
-
// Retrieve values using ACF if available, or direct term meta as fallback
|
|
1416
|
-
$featured_image = function_exists('get_field') ? get_field('featured_image', 'term_' . $term_id) : null;
|
|
1417
|
-
$flag = function_exists('get_field') ? get_field('flag', 'term_' . $term_id) : null;
|
|
1418
|
-
|
|
1419
|
-
if (empty($featured_image)) {
|
|
1420
|
-
$featured_image = get_term_meta($term_id, 'featured_image', true);
|
|
1421
|
-
}
|
|
1422
|
-
if (empty($flag)) {
|
|
1423
|
-
$flag = get_term_meta($term_id, 'flag', true);
|
|
1424
|
-
}
|
|
1425
|
-
|
|
1426
|
-
return array(
|
|
1427
|
-
'featured_image' => !empty($featured_image) ? $featured_image : '',
|
|
1428
|
-
'flag' => !empty($flag) ? $flag : ''
|
|
1429
|
-
);
|
|
1430
|
-
},
|
|
1431
|
-
'update_callback' => null,
|
|
1432
|
-
'schema' => null,
|
|
1433
|
-
));
|
|
1434
|
-
}
|
|
1435
|
-
});`;
|
|
1436
|
-
}
|
|
1437
|
-
|
|
1438
|
-
if (translatablePtKeys.length > 0 || registeredTaxonomies.length > 0) {
|
|
1439
|
-
const ptArrayStr = translatablePtKeys.map(pt => `'${pt}'`).join(', ');
|
|
1440
|
-
const taxArrayStr = registeredTaxonomies.map(tax => `'${tax}'`).join(', ');
|
|
1441
|
-
php += `\n\n// Automatically enable Polylang translation support for custom post types and taxonomies in WP Admin settings
|
|
1442
|
-
add_action('admin_init', function() {
|
|
1443
|
-
$options = get_option('polylang');
|
|
1444
|
-
if (is_array($options)) {
|
|
1445
|
-
$modified = false;
|
|
1446
|
-
|
|
1447
|
-
$cpts_to_enable = array(${ptArrayStr});
|
|
1448
|
-
if (!isset($options['post_types'])) {
|
|
1449
|
-
$options['post_types'] = array();
|
|
1450
|
-
}
|
|
1451
|
-
foreach ($cpts_to_enable as $cpt) {
|
|
1452
|
-
if (!in_array($cpt, $options['post_types'])) {
|
|
1453
|
-
$options['post_types'][] = $cpt;
|
|
1454
|
-
$modified = true;
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
|
|
1458
|
-
$taxonomies_to_enable = array(${taxArrayStr});
|
|
1459
|
-
if (!isset($options['taxonomies'])) {
|
|
1460
|
-
$options['taxonomies'] = array();
|
|
1461
|
-
}
|
|
1462
|
-
foreach ($taxonomies_to_enable as $tax) {
|
|
1463
|
-
if (!in_array($tax, $options['taxonomies'])) {
|
|
1464
|
-
$options['taxonomies'][] = $tax;
|
|
1465
|
-
$modified = true;
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
|
|
1469
|
-
if ($modified) {
|
|
1470
|
-
update_option('polylang', $options);
|
|
1471
|
-
flush_rewrite_rules();
|
|
1472
|
-
}
|
|
1473
|
-
}
|
|
1474
|
-
});
|
|
1475
|
-
|
|
1476
|
-
// Automatically filter out Polylang internal hidden taxonomies from relationship fields to prevent empty selections and '0' labels
|
|
1477
|
-
add_filter('get_object_taxonomies', function($taxonomies, $object, $output) {
|
|
1478
|
-
if (is_admin() && is_array($taxonomies)) {
|
|
1479
|
-
foreach ($taxonomies as $key => $tax) {
|
|
1480
|
-
$tax_name = is_object($tax) ? $tax->name : $tax;
|
|
1481
|
-
if ($tax_name === 'post_translations' || $tax_name === 'term_translations' || strpos($tax_name, 'pll_') === 0) {
|
|
1482
|
-
unset($taxonomies[$key]);
|
|
1483
|
-
}
|
|
1484
|
-
}
|
|
1485
|
-
if ($output !== 'objects') {
|
|
1486
|
-
$taxonomies = array_values($taxonomies);
|
|
1487
|
-
}
|
|
1488
|
-
}
|
|
1489
|
-
return $taxonomies;
|
|
1490
|
-
}, 99, 3);`;
|
|
1491
|
-
}
|
|
1492
|
-
return php;
|
|
1493
|
-
})()}
|
|
1494
|
-
`;
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1497
|
-
} catch (e) {}
|
|
1498
|
-
|
|
1499
|
-
let fontsEnqueue = '';
|
|
1500
|
-
let preconnectFilter = '';
|
|
1501
|
-
|
|
1502
|
-
if (googleFonts.length > 0) {
|
|
1503
|
-
const fontsParam = googleFonts
|
|
1504
|
-
.map((f) => encodeURIComponent(f))
|
|
1505
|
-
.join('&family=');
|
|
1506
|
-
fontsEnqueue = `
|
|
1507
|
-
// Enqueue Google Fonts (dynamic preset via wp.config.ts)
|
|
1508
|
-
wp_enqueue_style(
|
|
1509
|
-
'${config.textDomain}-google-fonts',
|
|
1510
|
-
'https://fonts.googleapis.com/css2?family=${fontsParam}&display=swap',
|
|
1511
|
-
array(),
|
|
1512
|
-
null
|
|
1513
|
-
);`;
|
|
1514
|
-
|
|
1515
|
-
preconnectFilter = `
|
|
1516
|
-
/**
|
|
1517
|
-
* Add preconnect resource hints for Google Fonts performance.
|
|
1518
|
-
*/
|
|
1519
|
-
function forgewp_google_fonts_resource_hints(array $urls, string $relation_type): array {
|
|
1520
|
-
if (wp_style_is('${config.textDomain}-google-fonts', 'queue') && 'preconnect' === $relation_type) {
|
|
1521
|
-
$urls[] = array(
|
|
1522
|
-
'href' => 'https://fonts.googleapis.com',
|
|
1523
|
-
'crossorigin' => 'anonymous',
|
|
1524
|
-
);
|
|
1525
|
-
$urls[] = array(
|
|
1526
|
-
'href' => 'https://fonts.gstatic.com',
|
|
1527
|
-
'crossorigin' => 'anonymous',
|
|
1528
|
-
);
|
|
1529
|
-
}
|
|
1530
|
-
return $urls;
|
|
1531
|
-
}
|
|
1532
|
-
add_filter('wp_resource_hints', 'forgewp_google_fonts_resource_hints', 10, 2);
|
|
1533
|
-
`;
|
|
1534
|
-
}
|
|
1535
|
-
|
|
1536
|
-
let blocksRegistration = '';
|
|
1537
|
-
if (blockSlugs.length > 0) {
|
|
1538
|
-
blocksRegistration = `
|
|
1539
|
-
/**
|
|
1540
|
-
* Register dynamic Gutenberg blocks compiled by ForgeWP.
|
|
1541
|
-
*/
|
|
1542
|
-
function forgewp_register_dynamic_blocks(): void {
|
|
1543
|
-
$blocks = array(${blockSlugs.map((s) => `'${s.name.replace('forgewp/', '')}'`).join(', ')});
|
|
1544
|
-
foreach ($blocks as $block) {
|
|
1545
|
-
register_block_type(__DIR__ . '/blocks/' . $block);
|
|
1546
|
-
}
|
|
1547
|
-
}
|
|
1548
|
-
add_action('init', 'forgewp_register_dynamic_blocks');
|
|
1549
|
-
|
|
1550
|
-
/**
|
|
1551
|
-
* Enqueue Block Editor JavaScript to dynamically render block interfaces.
|
|
1552
|
-
*/
|
|
1553
|
-
function forgewp_enqueue_block_editor_assets(): void {
|
|
1554
|
-
wp_enqueue_script(
|
|
1555
|
-
'forgewp-editor-script',
|
|
1556
|
-
get_template_directory_uri() . '/assets/forgewp-editor.js',
|
|
1557
|
-
array('wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render'),
|
|
1558
|
-
FORGEWP_THEME_VERSION,
|
|
1559
|
-
true
|
|
1560
|
-
);
|
|
1561
|
-
|
|
1562
|
-
wp_localize_script(
|
|
1563
|
-
'forgewp-editor-script',
|
|
1564
|
-
'forgeWpBlocks',
|
|
1565
|
-
json_decode('${JSON.stringify(blockSlugs).replace(/\\/g, '\\\\').replace(/'/g, "\\'")}')
|
|
1566
|
-
);
|
|
1567
|
-
}
|
|
1568
|
-
add_action('enqueue_block_editor_assets', 'forgewp_enqueue_block_editor_assets');
|
|
1569
|
-
`;
|
|
1570
|
-
}
|
|
1571
|
-
|
|
1572
|
-
let registerMenusPhp = '';
|
|
1573
|
-
const menuLocations = Object.keys(menus).filter((k) => !k.startsWith('_'));
|
|
1574
|
-
if (menuLocations.length > 0) {
|
|
1575
|
-
registerMenusPhp = `
|
|
1576
|
-
// Register custom navigation menus from sitemap config
|
|
1577
|
-
register_nav_menus(array(
|
|
1578
|
-
${menuLocations.map((loc) => ` '${loc}' => __('${loc.charAt(0).toUpperCase() + loc.slice(1)} Navigation', '${config.textDomain}'),`).join('\n')}
|
|
1579
|
-
));`;
|
|
1580
|
-
}
|
|
1581
|
-
|
|
1582
|
-
const pagesPhpArray = pagesToAutoCreate
|
|
1583
|
-
.map((p) => {
|
|
1584
|
-
return ` array(
|
|
1585
|
-
'title' => '${p.title.replace(/'/g, "\\'")}',
|
|
1586
|
-
'slug' => '${p.slug.replace(/'/g, "\\'")}',
|
|
1587
|
-
'template' => '${p.template.replace(/'/g, "\\'")}',
|
|
1588
|
-
)`;
|
|
1589
|
-
})
|
|
1590
|
-
.join(',\n');
|
|
1591
|
-
|
|
1592
|
-
const menuItemsPhpArray = Object.entries(menus)
|
|
1593
|
-
.filter(([loc]) => !loc.startsWith('_'))
|
|
1594
|
-
.map(([loc, items]) => {
|
|
1595
|
-
const itemsPhp = items
|
|
1596
|
-
.map((item) => {
|
|
1597
|
-
return ` array(
|
|
1598
|
-
'title' => '${item.title.replace(/'/g, "\\'")}',
|
|
1599
|
-
'url' => '${item.url.replace(/'/g, "\\'")}',
|
|
1600
|
-
)`;
|
|
1601
|
-
})
|
|
1602
|
-
.join(',\n');
|
|
1603
|
-
return ` '${loc}' => array(
|
|
1604
|
-
${itemsPhp}
|
|
1605
|
-
)`;
|
|
1606
|
-
})
|
|
1607
|
-
.join(',\n');
|
|
1608
|
-
|
|
1609
|
-
const schemaDefaultsPhpLines = [];
|
|
1610
|
-
for (const [templateSlug, schema] of Object.entries(schemas)) {
|
|
1611
|
-
const templateName = `page-${templateSlug}.php`;
|
|
1612
|
-
const fieldsPhpLines = [];
|
|
1613
|
-
for (const [key, field] of Object.entries(schema)) {
|
|
1614
|
-
if (field && typeof field.default !== 'undefined') {
|
|
1615
|
-
let defaultValStr = '';
|
|
1616
|
-
if (typeof field.default === 'object') {
|
|
1617
|
-
defaultValStr = JSON.stringify(field.default);
|
|
1618
|
-
} else {
|
|
1619
|
-
defaultValStr = String(field.default);
|
|
1620
|
-
}
|
|
1621
|
-
const escapedVal = defaultValStr.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
1622
|
-
fieldsPhpLines.push(` '${key}' => '${escapedVal}',`);
|
|
1623
|
-
}
|
|
1624
|
-
}
|
|
1625
|
-
if (fieldsPhpLines.length > 0) {
|
|
1626
|
-
schemaDefaultsPhpLines.push(` '${templateName}' => array(
|
|
1627
|
-
${fieldsPhpLines.join('\n')}
|
|
1628
|
-
)`);
|
|
1629
|
-
}
|
|
1630
|
-
}
|
|
1631
|
-
const schemaDefaultsPhp = schemaDefaultsPhpLines.join(',\n');
|
|
1632
|
-
|
|
1633
|
-
const autoCreationPhp = `
|
|
1634
|
-
/**
|
|
1635
|
-
* Automatically create routes defined in menus.json and assign custom page templates upon theme activation.
|
|
1636
|
-
*/
|
|
1637
|
-
function forgewp_auto_create_pages_and_menus(): void {
|
|
1638
|
-
$activated_option = '${config.textDomain}_activated_' . str_replace('.', '_', FORGEWP_THEME_VERSION);
|
|
1639
|
-
$has_run = get_option($activated_option) === 'yes';
|
|
1640
|
-
|
|
1641
|
-
$schema_defaults = array(
|
|
1642
|
-
${schemaDefaultsPhp}
|
|
1643
|
-
);
|
|
1644
|
-
|
|
1645
|
-
$pages = array(
|
|
1646
|
-
${pagesPhpArray}
|
|
1647
|
-
);
|
|
1648
|
-
|
|
1649
|
-
foreach ($pages as $p) {
|
|
1650
|
-
$existing_page = get_page_by_path($p['slug']);
|
|
1651
|
-
$page_id = 0;
|
|
1652
|
-
if (!$existing_page) {
|
|
1653
|
-
if (!$has_run) {
|
|
1654
|
-
$page_id = wp_insert_post(array(
|
|
1655
|
-
'post_title' => $p['title'],
|
|
1656
|
-
'post_name' => $p['slug'],
|
|
1657
|
-
'post_status' => 'publish',
|
|
1658
|
-
'post_type' => 'page',
|
|
1659
|
-
));
|
|
1660
|
-
if (!is_wp_error($page_id) && $page_id > 0) {
|
|
1661
|
-
update_post_meta($page_id, '_wp_page_template', $p['template']);
|
|
1662
|
-
}
|
|
1663
|
-
}
|
|
1664
|
-
} else {
|
|
1665
|
-
$page_id = $existing_page->ID;
|
|
1666
|
-
update_post_meta($page_id, '_wp_page_template', $p['template']);
|
|
1667
|
-
}
|
|
1668
|
-
|
|
1669
|
-
// Phase 3: Automatically seed default post meta fields if not already populated for ALL pages using this template
|
|
1670
|
-
if (isset($schema_defaults[$p['template']])) {
|
|
1671
|
-
$matching_pages = get_posts(array(
|
|
1672
|
-
'post_type' => 'page',
|
|
1673
|
-
'post_status'=> 'any',
|
|
1674
|
-
'lang' => '',
|
|
1675
|
-
'meta_query' => array(
|
|
1676
|
-
array(
|
|
1677
|
-
'key' => '_wp_page_template',
|
|
1678
|
-
'value' => $p['template'],
|
|
1679
|
-
'compare' => '='
|
|
1680
|
-
)
|
|
1681
|
-
),
|
|
1682
|
-
'posts_per_page' => -1
|
|
1683
|
-
));
|
|
1684
|
-
|
|
1685
|
-
$pages_to_seed = array();
|
|
1686
|
-
if ($page_id > 0) {
|
|
1687
|
-
$pages_to_seed[] = $page_id;
|
|
1688
|
-
}
|
|
1689
|
-
if (is_array($matching_pages)) {
|
|
1690
|
-
foreach ($matching_pages as $mp) {
|
|
1691
|
-
if (!in_array($mp->ID, $pages_to_seed)) {
|
|
1692
|
-
$pages_to_seed[] = $mp->ID;
|
|
1693
|
-
}
|
|
1694
|
-
}
|
|
1695
|
-
}
|
|
1696
|
-
|
|
1697
|
-
foreach ($pages_to_seed as $pid) {
|
|
1698
|
-
foreach ($schema_defaults[$p['template']] as $meta_key => $default_val) {
|
|
1699
|
-
// If it is a JSON repeater, decode and seed individual ACF sub-fields
|
|
1700
|
-
if (is_string($default_val) && (strpos($default_val, '[') === 0 || strpos($default_val, '{') === 0)) {
|
|
1701
|
-
$decoded = json_decode($default_val, true);
|
|
1702
|
-
if (is_array($decoded)) {
|
|
1703
|
-
// Check if the repeater is already populated by checking its count meta
|
|
1704
|
-
$current_count = get_post_meta($pid, $meta_key, true);
|
|
1705
|
-
if (empty($current_count) || !is_numeric($current_count) || intval($current_count) === 0) {
|
|
1706
|
-
// Seed the repeater count and values
|
|
1707
|
-
update_post_meta($pid, $meta_key, count($decoded));
|
|
1708
|
-
foreach ($decoded as $index => $row) {
|
|
1709
|
-
if (is_array($row)) {
|
|
1710
|
-
foreach ($row as $sub_key => $sub_val) {
|
|
1711
|
-
update_post_meta($pid, "{$meta_key}_{$index}_{$sub_key}", $sub_val);
|
|
1712
|
-
}
|
|
1713
|
-
}
|
|
1714
|
-
}
|
|
1715
|
-
|
|
1716
|
-
// Also store the raw JSON string as fallback for React direct hooks
|
|
1717
|
-
update_post_meta($pid, "{$meta_key}_json_fallback", $default_val);
|
|
1718
|
-
}
|
|
1719
|
-
|
|
1720
|
-
// ALWAYS enforce ACF key reference mappings for fields to display in WP Admin
|
|
1721
|
-
update_post_meta($pid, "_{$meta_key}", "field_{$meta_key}");
|
|
1722
|
-
$count_to_map = get_post_meta($pid, $meta_key, true);
|
|
1723
|
-
if (is_numeric($count_to_map)) {
|
|
1724
|
-
for ($index = 0; $index < intval($count_to_map); $index++) {
|
|
1725
|
-
foreach (array_keys($decoded[0] ?? array()) as $sub_key) {
|
|
1726
|
-
update_post_meta($pid, "_{$meta_key}_{$index}_{$sub_key}", "field_{$meta_key}_{$sub_key}");
|
|
1727
|
-
}
|
|
1728
|
-
}
|
|
1729
|
-
}
|
|
1730
|
-
continue;
|
|
1731
|
-
}
|
|
1732
|
-
}
|
|
1733
|
-
|
|
1734
|
-
$current_val = get_post_meta($pid, $meta_key, true);
|
|
1735
|
-
if (empty($current_val)) {
|
|
1736
|
-
update_post_meta($pid, $meta_key, $default_val);
|
|
1737
|
-
}
|
|
1738
|
-
}
|
|
1739
|
-
}
|
|
1740
|
-
}
|
|
1741
|
-
}
|
|
1742
|
-
|
|
1743
|
-
if (!$has_run) {
|
|
1744
|
-
$menu_structure = array(
|
|
1745
|
-
${menuItemsPhpArray}
|
|
1746
|
-
);
|
|
1747
|
-
|
|
1748
|
-
foreach ($menu_structure as $location => $items) {
|
|
1749
|
-
$menu_name = ucfirst($location) . ' Navigation';
|
|
1750
|
-
$menu_exists = wp_get_nav_menu_object($menu_name);
|
|
1751
|
-
|
|
1752
|
-
if (!$menu_exists) {
|
|
1753
|
-
$menu_id = wp_create_nav_menu($menu_name);
|
|
1754
|
-
if (!is_wp_error($menu_id)) {
|
|
1755
|
-
$locations = get_theme_mod('nav_menu_locations');
|
|
1756
|
-
if (!is_array($locations)) {
|
|
1757
|
-
$locations = array();
|
|
1758
|
-
}
|
|
1759
|
-
$locations[$location] = $menu_id;
|
|
1760
|
-
set_theme_mod('nav_menu_locations', $locations);
|
|
1761
|
-
|
|
1762
|
-
foreach ($items as $item) {
|
|
1763
|
-
$item_title = $item['title'];
|
|
1764
|
-
$item_url = $item['url'];
|
|
1765
|
-
|
|
1766
|
-
$object_id = 0;
|
|
1767
|
-
$object_type = 'custom';
|
|
1768
|
-
$target_url = $item_url;
|
|
1769
|
-
|
|
1770
|
-
if (strpos($item_url, '/') === 0) {
|
|
1771
|
-
$slug = trim($item_url, '/');
|
|
1772
|
-
if ($slug === '') {
|
|
1773
|
-
$target_url = home_url('/');
|
|
1774
|
-
} else {
|
|
1775
|
-
$page = get_page_by_path($slug);
|
|
1776
|
-
if ($page) {
|
|
1777
|
-
$object_id = $page->ID;
|
|
1778
|
-
$object_type = 'page';
|
|
1779
|
-
$target_url = get_permalink($page->ID);
|
|
1780
|
-
} else {
|
|
1781
|
-
$target_url = home_url($item_url);
|
|
1782
|
-
}
|
|
1783
|
-
}
|
|
1784
|
-
}
|
|
1785
|
-
|
|
1786
|
-
wp_update_nav_menu_item($menu_id, 0, array(
|
|
1787
|
-
'menu-item-title' => $item_title,
|
|
1788
|
-
'menu-item-url' => $target_url,
|
|
1789
|
-
'menu-item-object-id' => $object_id,
|
|
1790
|
-
'menu-item-object' => $object_type === 'page' ? 'page' : '',
|
|
1791
|
-
'menu-item-type' => $object_type === 'page' ? 'post_type' : 'custom',
|
|
1792
|
-
'menu-item-status' => 'publish',
|
|
1793
|
-
));
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
1796
|
-
}
|
|
1797
|
-
}
|
|
1798
|
-
update_option($activated_option, 'yes');
|
|
1799
|
-
}
|
|
1800
|
-
}
|
|
1801
|
-
add_action('after_switch_theme', 'forgewp_auto_create_pages_and_menus');
|
|
1802
|
-
add_action('init', 'forgewp_auto_create_pages_and_menus', 99);
|
|
1803
|
-
|
|
1804
|
-
/**
|
|
1805
|
-
* Register customizer settings for theme modifications (e.g., footer_text).
|
|
1806
|
-
* This allows theme mods to be managed via WordPress Customizer and WP Admin.
|
|
1807
|
-
*/
|
|
1808
|
-
function forgewp_register_theme_customizer_settings($wp_customize): void {
|
|
1809
|
-
// Register footer_text theme modification
|
|
1810
|
-
$wp_customize->add_setting(
|
|
1811
|
-
'footer_text',
|
|
1812
|
-
array(
|
|
1813
|
-
'default' => '',
|
|
1814
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
1815
|
-
)
|
|
1816
|
-
);
|
|
1817
|
-
|
|
1818
|
-
$wp_customize->add_control(
|
|
1819
|
-
'footer_text',
|
|
1820
|
-
array(
|
|
1821
|
-
'label' => __('Footer Text', '${config.textDomain}'),
|
|
1822
|
-
'section' => 'title_tagline',
|
|
1823
|
-
'type' => 'textarea',
|
|
1824
|
-
'description' => __('Custom text displayed in the theme footer.', '${config.textDomain}'),
|
|
1825
|
-
)
|
|
1826
|
-
);
|
|
1827
|
-
|
|
1828
|
-
// ── Social Media Section ──
|
|
1829
|
-
$wp_customize->add_section(
|
|
1830
|
-
'forgewp_social_section',
|
|
1831
|
-
array(
|
|
1832
|
-
'title' => __('Social Media Accounts', '${config.textDomain}'),
|
|
1833
|
-
'priority' => 35,
|
|
1834
|
-
'description' => __('Manage your social media profile URLs.', '${config.textDomain}'),
|
|
1835
|
-
)
|
|
1836
|
-
);
|
|
1837
|
-
|
|
1838
|
-
${socialSettingsPhp}
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
// ── Contact Information Section ──
|
|
1842
|
-
$wp_customize->add_section(
|
|
1843
|
-
'forgewp_contact_section',
|
|
1844
|
-
array(
|
|
1845
|
-
'title' => __('Contact Information', '${config.textDomain}'),
|
|
1846
|
-
'priority' => 36,
|
|
1847
|
-
'description' => __('Manage contact details used throughout the site.', '${config.textDomain}'),
|
|
1848
|
-
)
|
|
1849
|
-
);
|
|
1850
|
-
|
|
1851
|
-
// contact_phone
|
|
1852
|
-
$wp_customize->add_setting(
|
|
1853
|
-
'contact_phone',
|
|
1854
|
-
array(
|
|
1855
|
-
'type' => 'option',
|
|
1856
|
-
'default' => '',
|
|
1857
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
1858
|
-
)
|
|
1859
|
-
);
|
|
1860
|
-
$wp_customize->add_control(
|
|
1861
|
-
'contact_phone',
|
|
1862
|
-
array(
|
|
1863
|
-
'label' => __('Phone Number', '${config.textDomain}'),
|
|
1864
|
-
'section' => 'forgewp_contact_section',
|
|
1865
|
-
'type' => 'text',
|
|
1866
|
-
)
|
|
1867
|
-
);
|
|
1868
|
-
|
|
1869
|
-
// contact_email
|
|
1870
|
-
$wp_customize->add_setting(
|
|
1871
|
-
'contact_email',
|
|
1872
|
-
array(
|
|
1873
|
-
'type' => 'option',
|
|
1874
|
-
'default' => '',
|
|
1875
|
-
'sanitize_callback' => 'sanitize_email',
|
|
1876
|
-
)
|
|
1877
|
-
);
|
|
1878
|
-
$wp_customize->add_control(
|
|
1879
|
-
'contact_email',
|
|
1880
|
-
array(
|
|
1881
|
-
'label' => __('Email Address', '${config.textDomain}'),
|
|
1882
|
-
'section' => 'forgewp_contact_section',
|
|
1883
|
-
'type' => 'email',
|
|
1884
|
-
)
|
|
1885
|
-
);
|
|
1886
|
-
|
|
1887
|
-
// contact_address
|
|
1888
|
-
$wp_customize->add_setting(
|
|
1889
|
-
'contact_address',
|
|
1890
|
-
array(
|
|
1891
|
-
'type' => 'option',
|
|
1892
|
-
'default' => '',
|
|
1893
|
-
'sanitize_callback' => 'sanitize_textarea_field',
|
|
1894
|
-
)
|
|
1895
|
-
);
|
|
1896
|
-
$wp_customize->add_control(
|
|
1897
|
-
'contact_address',
|
|
1898
|
-
array(
|
|
1899
|
-
'label' => __('Address', '${config.textDomain}'),
|
|
1900
|
-
'section' => 'forgewp_contact_section',
|
|
1901
|
-
'type' => 'textarea',
|
|
1902
|
-
)
|
|
1903
|
-
);
|
|
1904
|
-
|
|
1905
|
-
// business_hours
|
|
1906
|
-
$wp_customize->add_setting(
|
|
1907
|
-
'business_hours',
|
|
1908
|
-
array(
|
|
1909
|
-
'type' => 'option',
|
|
1910
|
-
'default' => '',
|
|
1911
|
-
'sanitize_callback' => 'sanitize_text_field',
|
|
1912
|
-
)
|
|
1913
|
-
);
|
|
1914
|
-
$wp_customize->add_control(
|
|
1915
|
-
'business_hours',
|
|
1916
|
-
array(
|
|
1917
|
-
'label' => __('Business Hours', '${config.textDomain}'),
|
|
1918
|
-
'section' => 'forgewp_contact_section',
|
|
1919
|
-
'type' => 'text',
|
|
1920
|
-
)
|
|
1921
|
-
);
|
|
1922
|
-
}
|
|
1923
|
-
add_action('customize_register', 'forgewp_register_theme_customizer_settings');
|
|
1924
|
-
|
|
1925
|
-
/**
|
|
1926
|
-
* Set default footer_text on theme activation if not already set.
|
|
1927
|
-
*/
|
|
1928
|
-
function forgewp_set_default_footer_text(): void {
|
|
1929
|
-
if (!get_theme_mod('footer_text')) {
|
|
1930
|
-
set_theme_mod('footer_text', '© ' . date('Y') . ' ' . get_bloginfo('name') . '. ' . __('All rights reserved.', '${config.textDomain}'));
|
|
1931
|
-
}
|
|
1932
|
-
}
|
|
1933
|
-
add_action('after_switch_theme', 'forgewp_set_default_footer_text');
|
|
1934
|
-
`;
|
|
1935
|
-
|
|
1936
|
-
let hydrationEnqueue = '';
|
|
1937
|
-
if (hydrationData && hydrationData.mapping) {
|
|
1938
|
-
const mainJs = hydrationData.mainJsFile.replace(/^.*?[/\\]?assets[/\\]/, '');
|
|
1939
|
-
const manifestPairs = Object.entries(hydrationData.mapping)
|
|
1940
|
-
.map(([key, val]) => {
|
|
1941
|
-
const cleanVal = val.replace(/^.*?[/\\]?assets[/\\]/, '');
|
|
1942
|
-
return ` '${key}' => 'assets/${cleanVal}'`;
|
|
1943
|
-
})
|
|
1944
|
-
.join(',\n');
|
|
1945
|
-
|
|
1946
|
-
let headlessScript = '';
|
|
1947
|
-
if (config.headless) {
|
|
1948
|
-
const headlessApiUrl = config.headless.apiUrl || '';
|
|
1949
|
-
const headlessJwtAuth = !!config.headless.jwtAuth;
|
|
1950
|
-
headlessScript = `
|
|
1951
|
-
wp_add_inline_script(
|
|
1952
|
-
'${config.textDomain}-react-runtime',
|
|
1953
|
-
'window.FORGEWP_API_URL = ' . wp_json_encode('${headlessApiUrl}') . '; window.FORGEWP_JWT_AUTH = ${headlessJwtAuth ? 'true' : 'false'};',
|
|
1954
|
-
'before'
|
|
1955
|
-
);`;
|
|
1956
|
-
}
|
|
1957
|
-
|
|
1958
|
-
// Load mock site settings for hydration payloads
|
|
1959
|
-
let siteSettingsJson = {};
|
|
1960
|
-
const siteSettingsPath = path.join(themeRoot, 'cms', 'site-settings.json');
|
|
1961
|
-
if (existsSync(siteSettingsPath)) {
|
|
1962
|
-
try {
|
|
1963
|
-
siteSettingsJson = JSON.parse(readFileSync(siteSettingsPath, 'utf8'));
|
|
1964
|
-
} catch (e) {
|
|
1965
|
-
console.warn(
|
|
1966
|
-
'Failed to parse site-settings.json for hydration:',
|
|
1967
|
-
e.message,
|
|
1968
|
-
);
|
|
1969
|
-
}
|
|
1970
|
-
}
|
|
1971
|
-
|
|
1972
|
-
// Generate dynamic PHP for options collection
|
|
1973
|
-
const optionKeys = Object.keys(siteSettingsJson.options || {});
|
|
1974
|
-
const optionsPairs = optionKeys
|
|
1975
|
-
.map((key) => {
|
|
1976
|
-
const defaultVal = siteSettingsJson.options[key];
|
|
1977
|
-
let phpDefault = 'false';
|
|
1978
|
-
if (typeof defaultVal === 'string') {
|
|
1979
|
-
phpDefault = `'${defaultVal.replace(/'/g, "\\'")}'`;
|
|
1980
|
-
} else if (typeof defaultVal === 'number' || typeof defaultVal === 'boolean') {
|
|
1981
|
-
phpDefault = String(defaultVal);
|
|
1982
|
-
}
|
|
1983
|
-
return ` '${key}' => get_option('${key}', ${phpDefault})`;
|
|
1984
|
-
})
|
|
1985
|
-
.join(',\n');
|
|
1986
|
-
|
|
1987
|
-
// Generate dynamic PHP for theme_mods collection
|
|
1988
|
-
const themeModKeys = Object.keys(siteSettingsJson.theme_mods || {});
|
|
1989
|
-
const themeModsPairs = themeModKeys
|
|
1990
|
-
.map((key) => {
|
|
1991
|
-
const defaultVal = siteSettingsJson.theme_mods[key];
|
|
1992
|
-
let phpDefault = 'false';
|
|
1993
|
-
if (typeof defaultVal === 'string') {
|
|
1994
|
-
phpDefault = `'${defaultVal.replace(/'/g, "\\'")}'`;
|
|
1995
|
-
} else if (typeof defaultVal === 'number' || typeof defaultVal === 'boolean') {
|
|
1996
|
-
phpDefault = String(defaultVal);
|
|
1997
|
-
} else if (typeof defaultVal === 'object' && defaultVal !== null) {
|
|
1998
|
-
phpDefault = 'array()';
|
|
1999
|
-
}
|
|
2000
|
-
return ` '${key}' => get_theme_mod('${key}', ${phpDefault})`;
|
|
2001
|
-
})
|
|
2002
|
-
.join(',\n');
|
|
2003
|
-
|
|
2004
|
-
const pageTemplateSlugs = pagesToAutoCreate.map((p) =>
|
|
2005
|
-
p.template.replace(/^page-/, '').replace(/\.php$/, ''),
|
|
2006
|
-
);
|
|
2007
|
-
|
|
2008
|
-
let page_links_php = '';
|
|
2009
|
-
for (const slug of pageTemplateSlugs) {
|
|
2010
|
-
const safeVar = slug.replace(/[^a-zA-Z0-9]/g, '_');
|
|
2011
|
-
page_links_php += ` $get_pages_args_${safeVar} = array(
|
|
2012
|
-
'meta_key' => '_wp_page_template',
|
|
2013
|
-
'meta_value' => 'page-${slug}.php',
|
|
2014
|
-
'number' => 1
|
|
2015
|
-
);
|
|
2016
|
-
if (!empty($current_lang)) {
|
|
2017
|
-
$get_pages_args_${safeVar}['lang'] = $current_lang;
|
|
2018
|
-
}
|
|
2019
|
-
$matched_pages_${safeVar} = get_pages($get_pages_args_${safeVar});
|
|
2020
|
-
$page_links['${slug}'] = !empty($matched_pages_${safeVar}) ? get_permalink($matched_pages_${safeVar}[0]->ID) : '';\n`;
|
|
2021
|
-
}
|
|
2022
|
-
|
|
2023
|
-
const repeaterFields = {};
|
|
2024
|
-
for (const [templateSlug, schema] of Object.entries(schemas)) {
|
|
2025
|
-
for (const [key, field] of Object.entries(schema)) {
|
|
2026
|
-
if (field.type === 'repeater' && field.fields) {
|
|
2027
|
-
repeaterFields[key] = Object.keys(field.fields);
|
|
2028
|
-
}
|
|
2029
|
-
}
|
|
2030
|
-
}
|
|
2031
|
-
const repeaterFieldsPhp = Object.entries(repeaterFields)
|
|
2032
|
-
.map(([metaKey, subKeys]) => {
|
|
2033
|
-
const subKeysStr = subKeys.map((k) => `'${k}'`).join(', ');
|
|
2034
|
-
return ` '${metaKey}' => array(${subKeysStr})`;
|
|
2035
|
-
})
|
|
2036
|
-
.join(',\n');
|
|
2037
|
-
|
|
2038
|
-
hydrationEnqueue = `
|
|
2039
|
-
// Enqueue React runtime entrypoint and dynamic Selective Hydration assets
|
|
2040
|
-
$js_path = get_template_directory() . '/assets/${mainJs}';
|
|
2041
|
-
if (file_exists($js_path)) {
|
|
2042
|
-
wp_enqueue_script(
|
|
2043
|
-
'${config.textDomain}-react-runtime',
|
|
2044
|
-
$theme_uri . '/assets/${mainJs}',
|
|
2045
|
-
array(),
|
|
2046
|
-
FORGEWP_THEME_VERSION,
|
|
2047
|
-
true
|
|
2048
|
-
);
|
|
2049
|
-
${headlessScript}
|
|
2050
|
-
|
|
2051
|
-
$menu_locations = get_nav_menu_locations();
|
|
2052
|
-
$hydrated_menus = array();
|
|
2053
|
-
$registered_nav_menus = get_registered_nav_menus();
|
|
2054
|
-
$registered_locations = is_array($registered_nav_menus) ? array_keys($registered_nav_menus) : array();
|
|
2055
|
-
$current_lang = '';
|
|
2056
|
-
if (function_exists('pll_current_language')) {
|
|
2057
|
-
$current_lang = pll_current_language();
|
|
2058
|
-
} elseif (defined('ICL_LANGUAGE_CODE')) {
|
|
2059
|
-
$current_lang = ICL_LANGUAGE_CODE;
|
|
2060
|
-
}
|
|
2061
|
-
if (empty($current_lang)) {
|
|
2062
|
-
if (function_exists('pll_default_language')) {
|
|
2063
|
-
$current_lang = pll_default_language();
|
|
2064
|
-
} else {
|
|
2065
|
-
$current_lang = get_locale();
|
|
2066
|
-
if (strpos($current_lang, '_') !== false) {
|
|
2067
|
-
$parts = explode('_', $current_lang);
|
|
2068
|
-
$current_lang = $parts[0];
|
|
2069
|
-
}
|
|
2070
|
-
}
|
|
2071
|
-
}
|
|
2072
|
-
foreach ($registered_locations as $loc) {
|
|
2073
|
-
$base_loc = $loc;
|
|
2074
|
-
if (strpos($loc, '___') !== false) {
|
|
2075
|
-
$parts = explode('___', $loc);
|
|
2076
|
-
$base_loc = $parts[0];
|
|
2077
|
-
}
|
|
2078
|
-
|
|
2079
|
-
$menu_id = forgewp_get_menu_id_for_lang($base_loc, $current_lang);
|
|
2080
|
-
$raw_items = $menu_id ? wp_get_nav_menu_items($menu_id) : array();
|
|
2081
|
-
$menu_items = is_array($raw_items) ? $raw_items : array();
|
|
2082
|
-
$items_list = array();
|
|
2083
|
-
foreach ($menu_items as $item) {
|
|
2084
|
-
$items_list[] = array(
|
|
2085
|
-
'title' => $item->title,
|
|
2086
|
-
'url' => $item->url
|
|
2087
|
-
);
|
|
2088
|
-
}
|
|
2089
|
-
$hydrated_menus[$base_loc] = $items_list;
|
|
2090
|
-
if (!empty($current_lang)) {
|
|
2091
|
-
$hydrated_menus[$base_loc . '___' . $current_lang] = $items_list;
|
|
2092
|
-
}
|
|
2093
|
-
}
|
|
2094
|
-
|
|
2095
|
-
$page_links = array();
|
|
2096
|
-
${page_links_php}
|
|
2097
|
-
|
|
2098
|
-
// Inject hydration manifest mappings dynamically
|
|
2099
|
-
wp_add_inline_script(
|
|
2100
|
-
'${config.textDomain}-react-runtime',
|
|
2101
|
-
'window.forgeWpHydration = Object.assign(window.forgeWpHydration || {}, ' . wp_json_encode(array(
|
|
2102
|
-
'themeUri' => $theme_uri,
|
|
2103
|
-
'menus' => $hydrated_menus,
|
|
2104
|
-
'pageLinks' => $page_links,
|
|
2105
|
-
'loginField' => get_option( 'forgewp_auth_login_field', '${defaultLoginField}' ),${currentUserHydrationField}
|
|
2106
|
-
'manifest' => array(
|
|
2107
|
-
${manifestPairs}
|
|
2108
|
-
),
|
|
2109
|
-
'siteSettings' => array(
|
|
2110
|
-
'options' => array(
|
|
2111
|
-
${optionsPairs}
|
|
2112
|
-
),
|
|
2113
|
-
'theme_mods' => array(
|
|
2114
|
-
${themeModsPairs}
|
|
2115
|
-
),
|
|
2116
|
-
),
|
|
2117
|
-
), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE) . ');',
|
|
2118
|
-
'before'
|
|
2119
|
-
);
|
|
2120
|
-
|
|
2121
|
-
// Enqueue Micro-Hydrator orchestrator script
|
|
2122
|
-
wp_enqueue_script(
|
|
2123
|
-
'${config.textDomain}-hydrator',
|
|
2124
|
-
$theme_uri . '/assets/forgewp-hydrator.js',
|
|
2125
|
-
array('${config.textDomain}-react-runtime'),
|
|
2126
|
-
FORGEWP_THEME_VERSION,
|
|
2127
|
-
true
|
|
2128
|
-
);
|
|
2129
|
-
|
|
2130
|
-
// Integrate dynamic translation dictionary and Polylang / WPML multi-language data
|
|
2131
|
-
$i18n_keys = array(
|
|
2132
|
-
${i18nKeysPhp}
|
|
2133
|
-
);
|
|
2134
|
-
if (function_exists('pll_register_string')) {
|
|
2135
|
-
foreach ($i18n_keys as $key) {
|
|
2136
|
-
pll_register_string($key, $key, '${config.textDomain}');
|
|
2137
|
-
}
|
|
2138
|
-
}
|
|
2139
|
-
$current_lang = '';
|
|
2140
|
-
if (function_exists('pll_current_language')) {
|
|
2141
|
-
$current_lang = pll_current_language();
|
|
2142
|
-
}
|
|
2143
|
-
if (empty($current_lang)) {
|
|
2144
|
-
$current_lang = get_locale();
|
|
2145
|
-
}
|
|
2146
|
-
|
|
2147
|
-
$json_dict = array();
|
|
2148
|
-
$translations_file = get_template_directory() . '/translations.json';
|
|
2149
|
-
if (file_exists($translations_file)) {
|
|
2150
|
-
$json_data = json_decode(file_get_contents($translations_file), true);
|
|
2151
|
-
if (is_array($json_data) && isset($json_data[$current_lang])) {
|
|
2152
|
-
$json_dict = $json_data[$current_lang];
|
|
2153
|
-
}
|
|
2154
|
-
}
|
|
2155
|
-
|
|
2156
|
-
$translated_dict = array();
|
|
2157
|
-
if (is_array($json_dict)) {
|
|
2158
|
-
$translated_dict = $json_dict;
|
|
2159
|
-
}
|
|
2160
|
-
foreach ($i18n_keys as $key) {
|
|
2161
|
-
if (!isset($translated_dict[$key])) {
|
|
2162
|
-
$translated_dict[$key] = function_exists('pll__') ? pll__($key) : __($key, '${config.textDomain}');
|
|
2163
|
-
}
|
|
2164
|
-
}
|
|
2165
|
-
|
|
2166
|
-
$translations = array();
|
|
2167
|
-
$home_urls = array();
|
|
2168
|
-
if (function_exists('pll_the_languages')) {
|
|
2169
|
-
$langs = pll_the_languages(array('raw' => 1));
|
|
2170
|
-
if (is_array($langs)) {
|
|
2171
|
-
foreach ($langs as $l) {
|
|
2172
|
-
$translations[$l['slug']] = $l['url'];
|
|
2173
|
-
if (function_exists('pll_home_url')) {
|
|
2174
|
-
$home_urls[$l['slug']] = pll_home_url($l['slug']);
|
|
2175
|
-
}
|
|
2176
|
-
}
|
|
2177
|
-
}
|
|
2178
|
-
}
|
|
2179
|
-
if (empty($home_urls)) {
|
|
2180
|
-
$home_urls[$current_lang] = home_url('/');
|
|
2181
|
-
}
|
|
2182
|
-
|
|
2183
|
-
wp_add_inline_script(
|
|
2184
|
-
'${config.textDomain}-react-runtime',
|
|
2185
|
-
'window.forgeWpTranslations = ' . wp_json_encode(array(
|
|
2186
|
-
'currentLanguage' => $current_lang,
|
|
2187
|
-
'urls' => $translations,
|
|
2188
|
-
'homeUrls' => $home_urls,
|
|
2189
|
-
'translations' => $translated_dict
|
|
2190
|
-
), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE) . ';',
|
|
2191
|
-
'before'
|
|
2192
|
-
);
|
|
2193
|
-
|
|
2194
|
-
$post_custom_fields = array();
|
|
2195
|
-
$post_id = get_the_ID();
|
|
2196
|
-
if ($post_id > 0) {
|
|
2197
|
-
$registered_keys = array(${Array.from(uniqueMetaKeys).map(k => `'${k}'`).join(', ')});
|
|
2198
|
-
$rich_text_keys = array(${uniqueRichTextKeys.map(k => `'${k}'`).join(', ')});
|
|
2199
|
-
$repeater_fields_map = array(
|
|
2200
|
-
${repeaterFieldsPhp}
|
|
2201
|
-
);
|
|
2202
|
-
foreach ($registered_keys as $key) {
|
|
2203
|
-
if (array_key_exists($key, $repeater_fields_map)) {
|
|
2204
|
-
$post_custom_fields[$key] = forgewp_get_repeater_field($key, $repeater_fields_map[$key], $post_id);
|
|
2205
|
-
} else {
|
|
2206
|
-
$val = null;
|
|
2207
|
-
if (function_exists('get_field')) {
|
|
2208
|
-
$val = get_field($key, $post_id);
|
|
2209
|
-
}
|
|
2210
|
-
if ($val === null || $val === false) {
|
|
2211
|
-
$val = get_post_meta($post_id, $key, true);
|
|
2212
|
-
if (is_string($val) && (strpos($val, '[') === 0 || strpos($val, '{') === 0)) {
|
|
2213
|
-
$decoded = json_decode($val, true);
|
|
2214
|
-
if (json_last_error() === JSON_ERROR_NONE) {
|
|
2215
|
-
$val = $decoded;
|
|
2216
|
-
}
|
|
2217
|
-
}
|
|
2218
|
-
}
|
|
2219
|
-
if (in_array($key, $rich_text_keys) && is_string($val)) {
|
|
2220
|
-
$val = wpautop($val);
|
|
2221
|
-
}
|
|
2222
|
-
$post_custom_fields[$key] = $val;
|
|
2223
|
-
}
|
|
2224
|
-
}
|
|
2225
|
-
}
|
|
2226
|
-
|
|
2227
|
-
wp_add_inline_script(
|
|
2228
|
-
'${config.textDomain}-react-runtime',
|
|
2229
|
-
'if (!window.forgeWpHydration) { window.forgeWpHydration = {}; } window.forgeWpHydration.post = ' . wp_json_encode(array(
|
|
2230
|
-
'id' => $post_id,
|
|
2231
|
-
'customFields' => $post_custom_fields
|
|
2232
|
-
), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE) . ';',
|
|
2233
|
-
'before'
|
|
2234
|
-
);
|
|
2235
|
-
}
|
|
2236
|
-
|
|
2237
|
-
function ${config.textDomain.replace(/-/g, '_')}_script_loader_tag($tag, $handle, $src) {
|
|
2238
|
-
if (in_array($handle, array('${config.textDomain}-react-runtime', '${config.textDomain}-hydrator'), true)) {
|
|
2239
|
-
$tag = preg_replace_callback('/(<script\\b[^>]*src=[^>]*>)/i', function($matches) {
|
|
2240
|
-
$script_open = $matches[1];
|
|
2241
|
-
$script_open = preg_replace('/\\stype\\s*=\\s*([\\\'"])[^\\\'"]*\\1/i', '', $script_open);
|
|
2242
|
-
$script_open = preg_replace('/<script\\b/i', '<script type="module"', $script_open);
|
|
2243
|
-
return $script_open;
|
|
2244
|
-
}, $tag, 1);
|
|
2245
|
-
}
|
|
2246
|
-
return $tag;
|
|
2247
|
-
}
|
|
2248
|
-
add_filter('script_loader_tag', '${config.textDomain.replace(/-/g, '_')}_script_loader_tag', 10, 3);
|
|
2249
|
-
`;
|
|
2250
|
-
}
|
|
2251
|
-
|
|
2252
|
-
// ── Tier 2: Native Post Meta Registrations ──
|
|
2253
|
-
let nativeMetaRegisters = '';
|
|
2254
|
-
if (uniqueMetaKeys.size > 0) {
|
|
2255
|
-
const metaArray = Array.from(uniqueMetaKeys);
|
|
2256
|
-
|
|
2257
|
-
// Convert postTypes array into PHP array declaration
|
|
2258
|
-
const ptListStr = postTypes.length > 0 ? postTypes.map(pt => `'${pt}'`).join(', ') : '';
|
|
2259
|
-
const allPtExpr = ptListStr ? `array_merge(array('post', 'page'), array(${ptListStr}))` : `array('post', 'page')`;
|
|
2260
|
-
|
|
2261
|
-
nativeMetaRegisters = `
|
|
2262
|
-
/**
|
|
2263
|
-
* Register native post meta fields for all templates (Tier 2 Structured Content).
|
|
2264
|
-
*/
|
|
2265
|
-
function forgewp_register_custom_post_meta(): void {
|
|
2266
|
-
$post_types = ${allPtExpr};
|
|
2267
|
-
foreach ($post_types as $pt) {
|
|
2268
|
-
${metaArray.filter(key => !metaKeysToSkip.has(key)).map(key => {
|
|
2269
|
-
const phpType = metaKeyTypes[key];
|
|
2270
|
-
return ` register_post_meta($pt, '${key}', array(
|
|
2271
|
-
'show_in_rest' => true,
|
|
2272
|
-
'single' => true,
|
|
2273
|
-
'type' => '${phpType}',
|
|
2274
|
-
));`;
|
|
2275
|
-
}).join('\n')}
|
|
2276
|
-
}
|
|
2277
|
-
}
|
|
2278
|
-
add_action('init', 'forgewp_register_custom_post_meta');
|
|
2279
|
-
`;
|
|
2280
|
-
}
|
|
2281
|
-
|
|
2282
|
-
// ── Tier 3: Advanced Custom Fields (ACF) local field groups ──
|
|
2283
|
-
let acfFieldGroups = '';
|
|
2284
|
-
if (Object.keys(schemas).length > 0 || registeredTaxonomies.length > 0) {
|
|
2285
|
-
const groups = [];
|
|
2286
|
-
for (const [templateSlug, schema] of Object.entries(schemas)) {
|
|
2287
|
-
const groupKey = `group_${templateSlug.replace(/-/g, '_')}`;
|
|
2288
|
-
const groupTitle = templateSlug.split('-').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(' ') + ' Content';
|
|
2289
|
-
|
|
2290
|
-
let fieldsPhp = ' $fields = array();\n';
|
|
2291
|
-
for (const [key, field] of Object.entries(schema)) {
|
|
2292
|
-
fieldsPhp += generateDynamicAcfFieldPhp(key, field);
|
|
2293
|
-
}
|
|
2294
|
-
|
|
2295
|
-
let locationRule = '';
|
|
2296
|
-
if (templateSlug === 'front-page') {
|
|
2297
|
-
locationRule = `array(
|
|
2298
|
-
array(
|
|
2299
|
-
array(
|
|
2300
|
-
'param' => 'page_type',
|
|
2301
|
-
'operator' => '==',
|
|
2302
|
-
'value' => 'front_page',
|
|
2303
|
-
),
|
|
2304
|
-
),
|
|
2305
|
-
)`;
|
|
2306
|
-
} else if (templateSlug.startsWith('single-')) {
|
|
2307
|
-
let postType = templateSlug.replace('single-', '');
|
|
2308
|
-
if (postType.endsWith('-page')) {
|
|
2309
|
-
postType = postType.slice(0, -5);
|
|
2310
|
-
}
|
|
2311
|
-
locationRule = `array(
|
|
2312
|
-
array(
|
|
2313
|
-
array(
|
|
2314
|
-
'param' => 'post_type',
|
|
2315
|
-
'operator' => '==',
|
|
2316
|
-
'value' => '${postType}',
|
|
2317
|
-
),
|
|
2318
|
-
),
|
|
2319
|
-
)`;
|
|
2320
|
-
} else {
|
|
2321
|
-
const pageTemplateName = `page-${templateSlug}.php`;
|
|
2322
|
-
locationRule = `array(
|
|
2323
|
-
array(
|
|
2324
|
-
array(
|
|
2325
|
-
'param' => 'page_template',
|
|
2326
|
-
'operator' => '==',
|
|
2327
|
-
'value' => '${pageTemplateName}',
|
|
2328
|
-
),
|
|
2329
|
-
),
|
|
2330
|
-
)`;
|
|
2331
|
-
}
|
|
2332
|
-
|
|
2333
|
-
groups.push(` ${fieldsPhp}
|
|
2334
|
-
acf_add_local_field_group(array(
|
|
2335
|
-
'key' => '${groupKey}',
|
|
2336
|
-
'title' => '${groupTitle}',
|
|
2337
|
-
'fields' => $fields,
|
|
2338
|
-
'location' => ${locationRule},
|
|
2339
|
-
'menu_order' => 0,
|
|
2340
|
-
'position' => 'normal',
|
|
2341
|
-
'style' => 'default',
|
|
2342
|
-
'label_placement' => 'top',
|
|
2343
|
-
'instruction_placement' => 'label',
|
|
2344
|
-
'hide_on_screen' => '',
|
|
2345
|
-
'active' => true,
|
|
2346
|
-
'description' => 'Colocated React structured content fields managed by ForgeWP.',
|
|
2347
|
-
'show_in_rest' => true,
|
|
2348
|
-
));`);
|
|
2349
|
-
}
|
|
2350
|
-
|
|
2351
|
-
// Custom fields for custom taxonomies (like destination countries)
|
|
2352
|
-
for (const tax of registeredTaxonomies) {
|
|
2353
|
-
const taxGroupKey = `group_taxonomy_${tax}`;
|
|
2354
|
-
const taxGroupTitle = tax.split(/[_-]/).map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(' ') + ' Settings';
|
|
2355
|
-
groups.push(` // Custom fields for taxonomy: ${tax}
|
|
2356
|
-
acf_add_local_field_group(array(
|
|
2357
|
-
'key' => '${taxGroupKey}',
|
|
2358
|
-
'title' => '${taxGroupTitle}',
|
|
2359
|
-
'fields' => array(
|
|
2360
|
-
array(
|
|
2361
|
-
'key' => 'field_${tax}_featured_image',
|
|
2362
|
-
'label' => 'Featured Image',
|
|
2363
|
-
'name' => 'featured_image',
|
|
2364
|
-
'type' => 'image',
|
|
2365
|
-
'instructions' => 'Set the cover/featured image for this term.',
|
|
2366
|
-
'required' => 0,
|
|
2367
|
-
'conditional_logic' => 0,
|
|
2368
|
-
'return_format' => 'url',
|
|
2369
|
-
'preview_size' => 'medium',
|
|
2370
|
-
'library' => 'all',
|
|
2371
|
-
),
|
|
2372
|
-
array(
|
|
2373
|
-
'key' => 'field_${tax}_flag',
|
|
2374
|
-
'label' => 'Flag Emoji',
|
|
2375
|
-
'name' => 'flag',
|
|
2376
|
-
'type' => 'text',
|
|
2377
|
-
'instructions' => 'Set the flag emoji or brief code for this term (e.g. 🇩🇪 or DE).',
|
|
2378
|
-
'required' => 0,
|
|
2379
|
-
'conditional_logic' => 0,
|
|
2380
|
-
'default_value' => '🌍',
|
|
2381
|
-
),
|
|
2382
|
-
),
|
|
2383
|
-
'location' => array(
|
|
2384
|
-
array(
|
|
2385
|
-
array(
|
|
2386
|
-
'param' => 'taxonomy',
|
|
2387
|
-
'operator' => '==',
|
|
2388
|
-
'value' => '${tax}',
|
|
2389
|
-
),
|
|
2390
|
-
),
|
|
2391
|
-
),
|
|
2392
|
-
'menu_order' => 0,
|
|
2393
|
-
'position' => 'normal',
|
|
2394
|
-
'style' => 'default',
|
|
2395
|
-
'label_placement' => 'top',
|
|
2396
|
-
'instruction_placement' => 'label',
|
|
2397
|
-
'hide_on_screen' => '',
|
|
2398
|
-
'active' => true,
|
|
2399
|
-
'show_in_rest' => true,
|
|
2400
|
-
));`);
|
|
2401
|
-
}
|
|
2402
|
-
|
|
2403
|
-
acfFieldGroups = `
|
|
2404
|
-
/**
|
|
2405
|
-
* Register Advanced Custom Fields (ACF) local field groups (Tier 3 Interoperability Presentation).
|
|
2406
|
-
*/
|
|
2407
|
-
function forgewp_register_acf_field_groups(): void {
|
|
2408
|
-
if ( ! function_exists('acf_add_local_field_group') ) {
|
|
2409
|
-
return;
|
|
2410
|
-
}
|
|
2411
|
-
|
|
2412
|
-
${groups.join('\n\n')}
|
|
2413
|
-
}
|
|
2414
|
-
add_action('acf/init', 'forgewp_register_acf_field_groups');
|
|
2415
|
-
`;
|
|
2416
|
-
}
|
|
2417
|
-
|
|
2418
|
-
let queryEndpointsPhp = '';
|
|
2419
|
-
if (queries && queries.length > 0) {
|
|
2420
|
-
queryEndpointsPhp += `\n\n/**\n * ── Compiled REST Query Endpoints (Milestone 2) ──\n */\n`;
|
|
2421
|
-
queryEndpointsPhp += `
|
|
2422
|
-
if (!function_exists('forgewp_format_rest_post')) {
|
|
2423
|
-
function forgewp_format_rest_post(\$post) {
|
|
2424
|
-
\$id = \$post->ID;
|
|
2425
|
-
\$title = get_the_title(\$post);
|
|
2426
|
-
\$excerpt = get_the_excerpt(\$post);
|
|
2427
|
-
|
|
2428
|
-
\$content = apply_filters('the_content', \$post->post_content);
|
|
2429
|
-
\$date = get_the_date('', \$post);
|
|
2430
|
-
\$author = get_the_author_meta('display_name', \$post->post_author);
|
|
2431
|
-
|
|
2432
|
-
\$featured_image = 'https://picsum.photos/seed/forgewp/1200/630';
|
|
2433
|
-
if (has_post_thumbnail(\$post)) {
|
|
2434
|
-
\$thumbnail_url = get_the_post_thumbnail_url(\$post, 'full');
|
|
2435
|
-
if (\$thumbnail_url) {
|
|
2436
|
-
\$featured_image = \$thumbnail_url;
|
|
2437
|
-
}
|
|
2438
|
-
}
|
|
2439
|
-
|
|
2440
|
-
\$permalink = get_permalink(\$post);
|
|
2441
|
-
|
|
2442
|
-
\$custom_fields = array();
|
|
2443
|
-
\$meta = get_post_meta(\$id);
|
|
2444
|
-
if (is_array(\$meta)) {
|
|
2445
|
-
foreach (\$meta as \$key => \$values) {
|
|
2446
|
-
\$val = isset(\$values[0]) ? maybe_unserialize(\$values[0]) : '';
|
|
2447
|
-
\$custom_fields[\$key] = \$val;
|
|
2448
|
-
}
|
|
2449
|
-
}
|
|
2450
|
-
|
|
2451
|
-
if (function_exists('get_fields')) {
|
|
2452
|
-
\$acf_fields = get_fields(\$id);
|
|
2453
|
-
if (is_array(\$acf_fields)) {
|
|
2454
|
-
\$custom_fields = array_merge(\$custom_fields, \$acf_fields);
|
|
2455
|
-
}
|
|
2456
|
-
}
|
|
2457
|
-
|
|
2458
|
-
\$terms_data = array();
|
|
2459
|
-
\$taxonomies = get_object_taxonomies(\$post->post_type);
|
|
2460
|
-
if (is_array(\$taxonomies)) {
|
|
2461
|
-
foreach (\$taxonomies as \$taxonomy) {
|
|
2462
|
-
\$terms = get_the_terms(\$post, \$taxonomy);
|
|
2463
|
-
if (is_array(\$terms)) {
|
|
2464
|
-
\$terms_data[\$taxonomy] = array();
|
|
2465
|
-
foreach (\$terms as \$t) {
|
|
2466
|
-
\$terms_data[\$taxonomy][] = array(
|
|
2467
|
-
'id' => \$t->term_id,
|
|
2468
|
-
'slug' => \$t->slug,
|
|
2469
|
-
'name' => html_entity_decode(\$t->name, ENT_QUOTES, 'UTF-8'),
|
|
2470
|
-
);
|
|
2471
|
-
}
|
|
2472
|
-
}
|
|
2473
|
-
}
|
|
2474
|
-
}
|
|
2475
|
-
|
|
2476
|
-
return array(
|
|
2477
|
-
'ID' => \$id,
|
|
2478
|
-
'title' => \$title,
|
|
2479
|
-
'excerpt' => \$excerpt,
|
|
2480
|
-
'content' => \$content,
|
|
2481
|
-
'date' => \$date,
|
|
2482
|
-
'author' => \$author,
|
|
2483
|
-
'featuredImage' => \$featured_image,
|
|
2484
|
-
'permalink' => \$permalink,
|
|
2485
|
-
'customFields' => \$custom_fields,
|
|
2486
|
-
'_terms' => \$terms_data,
|
|
2487
|
-
'post_type' => \$post->post_type,
|
|
2488
|
-
);
|
|
2489
|
-
}
|
|
2490
|
-
}
|
|
2491
|
-
`;
|
|
2492
|
-
|
|
2493
|
-
queryEndpointsPhp += `
|
|
2494
|
-
add_action('rest_api_init', function() {`;
|
|
2495
|
-
for (const q of queries) {
|
|
2496
|
-
const cleanId = q.queryId.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');
|
|
2497
|
-
const querySlug = 'query-' + cleanId;
|
|
2498
|
-
const callbackName = 'forgewp_rest_query_' + cleanId.replace(/-/g, '_');
|
|
2499
|
-
queryEndpointsPhp += `
|
|
2500
|
-
register_rest_route('forgewp/v1', '/${querySlug}', array(
|
|
2501
|
-
'methods' => 'GET',
|
|
2502
|
-
'callback' => '${callbackName}',
|
|
2503
|
-
'permission_callback' => '__return_true',
|
|
2504
|
-
));`;
|
|
2505
|
-
}
|
|
2506
|
-
queryEndpointsPhp += `
|
|
2507
|
-
});
|
|
2508
|
-
`;
|
|
2509
|
-
|
|
2510
|
-
for (const q of queries) {
|
|
2511
|
-
const cleanId = q.queryId.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');
|
|
2512
|
-
const callbackName = 'forgewp_rest_query_' + cleanId.replace(/-/g, '_');
|
|
2513
|
-
const phpArgs = generatePhpArgs(q);
|
|
2514
|
-
|
|
2515
|
-
queryEndpointsPhp += `
|
|
2516
|
-
function ${callbackName}(WP_REST_Request \$request) {
|
|
2517
|
-
\$paged = \$request->get_param('page') ? intval(\$request->get_param('page')) : 1;
|
|
2518
|
-
\$s = \$request->get_param('search');
|
|
2519
|
-
if (empty(\$s)) {
|
|
2520
|
-
\$s = \$request->get_param('s');
|
|
2521
|
-
}
|
|
2522
|
-
|
|
2523
|
-
${phpArgs}
|
|
2524
|
-
|
|
2525
|
-
if (!empty(\$s)) {
|
|
2526
|
-
\$args['s'] = sanitize_text_field(\$s);
|
|
2527
|
-
}
|
|
2528
|
-
|
|
2529
|
-
\$lang = \$request->get_param('lang');
|
|
2530
|
-
if (!empty(\$lang)) {
|
|
2531
|
-
\$args['lang'] = sanitize_text_field(\$lang);
|
|
2532
|
-
}
|
|
2533
|
-
|
|
2534
|
-
\$query = new WP_Query(\$args);
|
|
2535
|
-
\$posts = array();
|
|
2536
|
-
if (\$query->have_posts()) {
|
|
2537
|
-
while (\$query->have_posts()) {
|
|
2538
|
-
\$query->the_post();
|
|
2539
|
-
\$posts[] = forgewp_format_rest_post(\$query->post);
|
|
2540
|
-
}
|
|
2541
|
-
wp_reset_postdata();
|
|
2542
|
-
}
|
|
2543
|
-
|
|
2544
|
-
return new WP_REST_Response(array(
|
|
2545
|
-
'posts' => \$posts,
|
|
2546
|
-
'total' => intval(\$query->found_posts),
|
|
2547
|
-
'totalPages' => intval(\$query->max_num_pages),
|
|
2548
|
-
), 200);
|
|
2549
|
-
}
|
|
2550
|
-
`;
|
|
2551
|
-
}
|
|
2552
|
-
}
|
|
2553
|
-
|
|
2554
|
-
return `<?php
|
|
2555
|
-
/**
|
|
2556
|
-
* ${config.name} — generated by ForgeWP
|
|
2557
|
-
*
|
|
2558
|
-
* @package ${config.textDomain}
|
|
2559
|
-
*/
|
|
2560
|
-
|
|
2561
|
-
if (! defined('ABSPATH')) {
|
|
2562
|
-
exit;
|
|
2563
|
-
}
|
|
2564
|
-
|
|
2565
|
-
define('FORGEWP_THEME_VERSION', '${version}');
|
|
2566
|
-
|
|
2567
|
-
/**
|
|
2568
|
-
* Helper to fetch any repeater field, bypassing ACF internal formatting if needed.
|
|
2569
|
-
*/
|
|
2570
|
-
function forgewp_get_repeater_field($meta_key, $sub_field_keys, $post_id = null) {
|
|
2571
|
-
if (!$post_id) {
|
|
2572
|
-
$post_id = get_the_ID();
|
|
2573
|
-
}
|
|
2574
|
-
if (!$post_id) {
|
|
2575
|
-
return array();
|
|
2576
|
-
}
|
|
2577
|
-
|
|
2578
|
-
$rows = array();
|
|
2579
|
-
|
|
2580
|
-
// 1. Try get_field first
|
|
2581
|
-
if (function_exists('get_field')) {
|
|
2582
|
-
$val = get_field($meta_key, $post_id);
|
|
2583
|
-
if (is_array($val) && !empty($val)) {
|
|
2584
|
-
$rows = $val;
|
|
2585
|
-
}
|
|
2586
|
-
}
|
|
2587
|
-
|
|
2588
|
-
// 2. Direct DB query using ACF structure
|
|
2589
|
-
if (empty($rows)) {
|
|
2590
|
-
$count = get_post_meta($post_id, $meta_key, true);
|
|
2591
|
-
if (is_numeric($count) && intval($count) > 0) {
|
|
2592
|
-
$count = intval($count);
|
|
2593
|
-
for ($i = 0; $i < $count; $i++) {
|
|
2594
|
-
$row = array();
|
|
2595
|
-
foreach ($sub_field_keys as $sub_key) {
|
|
2596
|
-
$sub_val = get_post_meta($post_id, "{$meta_key}_{$i}_{$sub_key}", true);
|
|
2597
|
-
if (is_string($sub_val) && (strpos($sub_val, '[') === 0 || strpos($sub_val, '{') === 0)) {
|
|
2598
|
-
$decoded = json_decode($sub_val, true);
|
|
2599
|
-
if (is_array($decoded)) {
|
|
2600
|
-
$sub_val = $decoded;
|
|
2601
|
-
}
|
|
2602
|
-
}
|
|
2603
|
-
$row[$sub_key] = $sub_val;
|
|
2604
|
-
}
|
|
2605
|
-
$rows[] = $row;
|
|
2606
|
-
}
|
|
2607
|
-
}
|
|
2608
|
-
}
|
|
2609
|
-
|
|
2610
|
-
// 3. Fallback to json_fallback
|
|
2611
|
-
if (empty($rows)) {
|
|
2612
|
-
$fallback = get_post_meta($post_id, "{$meta_key}_json_fallback", true);
|
|
2613
|
-
if (is_string($fallback) && !empty($fallback)) {
|
|
2614
|
-
$decoded = json_decode($fallback, true);
|
|
2615
|
-
if (is_array($decoded)) {
|
|
2616
|
-
$rows = $decoded;
|
|
2617
|
-
}
|
|
2618
|
-
}
|
|
2619
|
-
}
|
|
2620
|
-
|
|
2621
|
-
// 4. Raw JSON fallback
|
|
2622
|
-
if (empty($rows)) {
|
|
2623
|
-
$raw_meta = get_post_meta($post_id, $meta_key, true);
|
|
2624
|
-
if (is_string($raw_meta) && !empty($raw_meta)) {
|
|
2625
|
-
$decoded = json_decode($raw_meta, true);
|
|
2626
|
-
if (is_array($decoded)) {
|
|
2627
|
-
$rows = $decoded;
|
|
2628
|
-
}
|
|
2629
|
-
}
|
|
2630
|
-
}
|
|
2631
|
-
|
|
2632
|
-
// Normalize rows to resolve image arrays/IDs to URLs
|
|
2633
|
-
if (is_array($rows)) {
|
|
2634
|
-
foreach ($rows as $index => $row) {
|
|
2635
|
-
if (is_array($row)) {
|
|
2636
|
-
foreach ($row as $sub_key => $sub_val) {
|
|
2637
|
-
if (is_string($sub_val) && (strpos($sub_val, '[') === 0 || strpos($sub_val, '{') === 0)) {
|
|
2638
|
-
$decoded = json_decode($sub_val, true);
|
|
2639
|
-
if (is_array($decoded)) {
|
|
2640
|
-
$sub_val = $decoded;
|
|
2641
|
-
}
|
|
2642
|
-
}
|
|
2643
|
-
|
|
2644
|
-
if (is_array($sub_val) && isset($sub_val['url'])) {
|
|
2645
|
-
$sub_val = $sub_val['url'];
|
|
2646
|
-
} elseif (is_numeric($sub_val) && intval($sub_val) > 0 && get_post_type(intval($sub_val)) === 'attachment') {
|
|
2647
|
-
$url = wp_get_attachment_image_url(intval($sub_val), 'full');
|
|
2648
|
-
if ($url) {
|
|
2649
|
-
$sub_val = $url;
|
|
2650
|
-
}
|
|
2651
|
-
}
|
|
2652
|
-
|
|
2653
|
-
$rows[$index][$sub_key] = $sub_val;
|
|
2654
|
-
}
|
|
2655
|
-
}
|
|
2656
|
-
}
|
|
2657
|
-
}
|
|
2658
|
-
|
|
2659
|
-
return is_array($rows) ? $rows : array();
|
|
2660
|
-
}
|
|
2661
|
-
|
|
2662
|
-
/**
|
|
2663
|
-
* Helper to fetch and format page/post meta fields, resolving ACF formatting and attachments.
|
|
2664
|
-
*/
|
|
2665
|
-
function forgewp_get_meta_value($key, $default_val = '', $is_rich_text = false) {
|
|
2666
|
-
$post_id = get_the_ID();
|
|
2667
|
-
if (!$post_id) {
|
|
2668
|
-
return $default_val;
|
|
2669
|
-
}
|
|
2670
|
-
|
|
2671
|
-
$val = null;
|
|
2672
|
-
|
|
2673
|
-
// 1. Try get_field first if ACF is active
|
|
2674
|
-
if (function_exists('get_field')) {
|
|
2675
|
-
$val = get_field($key, $post_id);
|
|
2676
|
-
}
|
|
2677
|
-
|
|
2678
|
-
// 2. Direct get_post_meta fallback
|
|
2679
|
-
if ($val === null || $val === false) {
|
|
2680
|
-
$val = get_post_meta($post_id, $key, true);
|
|
2681
|
-
}
|
|
2682
|
-
|
|
2683
|
-
// 3. Fallback to default if empty
|
|
2684
|
-
if ($val === null || $val === false || $val === '') {
|
|
2685
|
-
return $default_val;
|
|
2686
|
-
}
|
|
2687
|
-
|
|
2688
|
-
// 4. Resolve Image Arrays / Objects (ACF formats)
|
|
2689
|
-
if (is_array($val) && isset($val['url'])) {
|
|
2690
|
-
$val = $val['url'];
|
|
2691
|
-
}
|
|
2692
|
-
// 5. Resolve Attachment IDs to URLs
|
|
2693
|
-
elseif (is_numeric($val) && intval($val) > 0 && get_post_type(intval($val)) === 'attachment') {
|
|
2694
|
-
$url = wp_get_attachment_image_url(intval($val), 'full');
|
|
2695
|
-
if ($url) {
|
|
2696
|
-
$val = $url;
|
|
2697
|
-
}
|
|
2698
|
-
}
|
|
2699
|
-
|
|
2700
|
-
// 6. Format and escape
|
|
2701
|
-
if ($is_rich_text) {
|
|
2702
|
-
return wpautop($val);
|
|
2703
|
-
} else {
|
|
2704
|
-
return esc_html($val);
|
|
2705
|
-
}
|
|
2706
|
-
}
|
|
2707
|
-
|
|
2708
|
-
/**
|
|
2709
|
-
* Resolves the menu ID for a location and language, using WordPress nav menu locations.
|
|
2710
|
-
*/
|
|
2711
|
-
function forgewp_get_menu_id_for_lang($location, $lang = '') {
|
|
2712
|
-
$locations = get_nav_menu_locations();
|
|
2713
|
-
|
|
2714
|
-
if (empty($locations) || !is_array($locations)) {
|
|
2715
|
-
return null;
|
|
2716
|
-
}
|
|
2717
|
-
|
|
2718
|
-
// 1. Try to find the language-specific location key (e.g. primary___de)
|
|
2719
|
-
if (!empty($lang)) {
|
|
2720
|
-
$lang_key = $location . '___' . $lang;
|
|
2721
|
-
if (isset($locations[$lang_key])) {
|
|
2722
|
-
return $locations[$lang_key];
|
|
2723
|
-
}
|
|
2724
|
-
}
|
|
2725
|
-
|
|
2726
|
-
// 2. Try to find the base location key (e.g. primary)
|
|
2727
|
-
if (isset($locations[$location])) {
|
|
2728
|
-
return $locations[$location];
|
|
2729
|
-
}
|
|
2730
|
-
|
|
2731
|
-
// 3. Fallback to any matching key in the locations array
|
|
2732
|
-
foreach ($locations as $key => $val) {
|
|
2733
|
-
if ($key === $location || strpos($key, $location . '___') === 0) {
|
|
2734
|
-
return $val;
|
|
2735
|
-
}
|
|
2736
|
-
}
|
|
2737
|
-
|
|
2738
|
-
return null;
|
|
2739
|
-
}
|
|
2740
|
-
|
|
2741
|
-
/**
|
|
2742
|
-
* Enqueue compiled theme assets.
|
|
2743
|
-
*/
|
|
2744
|
-
function forgewp_enqueue_assets(): void {
|
|
2745
|
-
$theme_uri = get_template_directory_uri();
|
|
2746
|
-
$css_path = get_template_directory() . '/assets/${css}';
|
|
2747
|
-
${fontsEnqueue}
|
|
2748
|
-
|
|
2749
|
-
if (file_exists($css_path)) {
|
|
2750
|
-
wp_enqueue_style(
|
|
2751
|
-
'${config.textDomain}-app',
|
|
2752
|
-
$theme_uri . '/assets/${css}',
|
|
2753
|
-
array(),
|
|
2754
|
-
FORGEWP_THEME_VERSION
|
|
2755
|
-
);
|
|
2756
|
-
}
|
|
2757
|
-
${hydrationEnqueue}
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
}
|
|
2761
|
-
add_action('wp_enqueue_scripts', 'forgewp_enqueue_assets');
|
|
2762
|
-
|
|
2763
|
-
/**
|
|
2764
|
-
* Theme supports.
|
|
2765
|
-
*/
|
|
2766
|
-
function forgewp_theme_setup(): void {
|
|
2767
|
-
add_theme_support('title-tag');
|
|
2768
|
-
add_theme_support('post-thumbnails');
|
|
2769
|
-
add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script'));
|
|
2770
|
-
${wpBlockStylesLine} add_theme_support('editor-styles');
|
|
2771
|
-
|
|
2772
|
-
// Load compiled theme stylesheet inside Gutenberg Block Editor
|
|
2773
|
-
add_editor_style('assets/${css}');
|
|
2774
|
-
${registerMenusPhp}
|
|
2775
|
-
}
|
|
2776
|
-
add_action('after_setup_theme', 'forgewp_theme_setup');
|
|
2777
|
-
${preconnectFilter}${blocksRegistration}${cptRegistration}${autoCreationPhp}${nativeMetaRegisters}${acfFieldGroups}
|
|
2778
|
-
|
|
2779
|
-
/**
|
|
2780
|
-
* Resolves the active template head HTML file path dynamically.
|
|
2781
|
-
*/
|
|
2782
|
-
function forgewp_resolve_head_target() {
|
|
2783
|
-
$dir = get_template_directory() . '/forgewp-static';
|
|
2784
|
-
|
|
2785
|
-
// 1. Custom Page Template
|
|
2786
|
-
$template_slug = get_page_template_slug();
|
|
2787
|
-
if ($template_slug) {
|
|
2788
|
-
$clean_slug = str_replace(array('page-', '.php'), '', $template_slug);
|
|
2789
|
-
$target = "{$dir}/template-{$clean_slug}-head.html";
|
|
2790
|
-
if (file_exists($target)) {
|
|
2791
|
-
return $target;
|
|
2792
|
-
}
|
|
2793
|
-
}
|
|
2794
|
-
|
|
2795
|
-
// 2. Singular Custom Post Type / Post / Page
|
|
2796
|
-
if (is_singular()) {
|
|
2797
|
-
$post_type = get_post_type();
|
|
2798
|
-
$target = "{$dir}/single-{$post_type}-head.html";
|
|
2799
|
-
if (file_exists($target)) {
|
|
2800
|
-
return $target;
|
|
2801
|
-
}
|
|
2802
|
-
$target = "{$dir}/single-head.html";
|
|
2803
|
-
if (file_exists($target)) {
|
|
2804
|
-
return $target;
|
|
2805
|
-
}
|
|
2806
|
-
}
|
|
2807
|
-
|
|
2808
|
-
// 3. Taxonomy Archive
|
|
2809
|
-
if (is_tax()) {
|
|
2810
|
-
$tax = get_query_var('taxonomy');
|
|
2811
|
-
$target = "{$dir}/taxonomy-{$tax}-head.html";
|
|
2812
|
-
if (file_exists($target)) {
|
|
2813
|
-
return $target;
|
|
2814
|
-
}
|
|
2815
|
-
$target = "{$dir}/taxonomy-head.html";
|
|
2816
|
-
if (file_exists($target)) {
|
|
2817
|
-
return $target;
|
|
2818
|
-
}
|
|
2819
|
-
}
|
|
2820
|
-
|
|
2821
|
-
// 4. Post Type Archive
|
|
2822
|
-
if (is_post_type_archive()) {
|
|
2823
|
-
$post_type = get_query_var('post_type');
|
|
2824
|
-
$target = "{$dir}/archive-{$post_type}-head.html";
|
|
2825
|
-
if (file_exists($target)) {
|
|
2826
|
-
return $target;
|
|
2827
|
-
}
|
|
2828
|
-
}
|
|
2829
|
-
|
|
2830
|
-
// 5. General Archive / Category / Tag
|
|
2831
|
-
if (is_archive()) {
|
|
2832
|
-
$target = "{$dir}/archive-head.html";
|
|
2833
|
-
if (file_exists($target)) {
|
|
2834
|
-
return $target;
|
|
2835
|
-
}
|
|
2836
|
-
}
|
|
2837
|
-
|
|
2838
|
-
// 6. Default Head
|
|
2839
|
-
return "{$dir}/head.html";
|
|
2840
|
-
}
|
|
2841
|
-
|
|
2842
|
-
/**
|
|
2843
|
-
* Custom document title filter for ForgeWP.
|
|
2844
|
-
* Maps title enqueued via <WpHead /> in React to WordPress.
|
|
2845
|
-
*/
|
|
2846
|
-
function forgewp_custom_document_title( $title ) {
|
|
2847
|
-
if ( defined('WPSEO_VERSION') || class_exists('RankMath') || class_exists('All_in_One_SEO_Pack') || defined('AIOSEO_VERSION') ) {
|
|
2848
|
-
return $title;
|
|
2849
|
-
}
|
|
2850
|
-
$target = forgewp_resolve_head_target();
|
|
2851
|
-
if ( file_exists( $target ) ) {
|
|
2852
|
-
ob_start();
|
|
2853
|
-
include $target;
|
|
2854
|
-
$content = ob_get_clean();
|
|
2855
|
-
if ( preg_match( '/<title>(.*?)<\\/title>/is', $content, $matches ) ) {
|
|
2856
|
-
return html_entity_decode( trim( $matches[1] ), ENT_QUOTES, 'UTF-8' );
|
|
2857
|
-
}
|
|
2858
|
-
}
|
|
2859
|
-
return $title;
|
|
2860
|
-
}
|
|
2861
|
-
add_filter( 'pre_get_document_title', 'forgewp_custom_document_title', 999 );
|
|
2862
|
-
${seoPluginsPhp}
|
|
2863
|
-
|
|
2864
|
-
/**
|
|
2865
|
-
* Disable WordPress legacy emoji conversion to match native client-side rendering.
|
|
2866
|
-
*/
|
|
2867
|
-
function forgewp_disable_emojis(): void {
|
|
2868
|
-
remove_action('wp_head', 'print_emoji_detection_script', 7);
|
|
2869
|
-
remove_action('admin_print_scripts', 'print_emoji_detection_script');
|
|
2870
|
-
remove_action('wp_print_styles', 'print_emoji_styles');
|
|
2871
|
-
remove_action('admin_print_styles', 'print_emoji_styles');
|
|
2872
|
-
remove_filter('the_content_feed', 'wp_staticize_emoji');
|
|
2873
|
-
remove_filter('comment_text_rss', 'wp_staticize_emoji');
|
|
2874
|
-
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
|
|
2875
|
-
add_filter('tiny_mce_plugins', 'forgewp_disable_emojis_tinymce');
|
|
2876
|
-
add_filter('wp_resource_hints', 'forgewp_disable_emojis_remove_dns_prefetch', 10, 2);
|
|
2877
|
-
}
|
|
2878
|
-
add_action('init', 'forgewp_disable_emojis');
|
|
2879
|
-
|
|
2880
|
-
function forgewp_disable_emojis_tinymce($plugins) {
|
|
2881
|
-
return is_array($plugins) ? array_diff($plugins, array('wpemoji')) : array();
|
|
2882
|
-
}
|
|
2883
|
-
|
|
2884
|
-
function forgewp_disable_emojis_remove_dns_prefetch($urls, $relation_type) {
|
|
2885
|
-
if ('dns-prefetch' === $relation_type) {
|
|
2886
|
-
$emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/2.2.1/svg/');
|
|
2887
|
-
$urls = array_diff($urls, array($emoji_svg_url));
|
|
2888
|
-
}
|
|
2889
|
-
return $urls;
|
|
2890
|
-
}
|
|
2891
|
-
|
|
2892
|
-
/**
|
|
2893
|
-
* Isomorphic static translation bridge for ForgeWP.
|
|
2894
|
-
* Hooks into WordPress gettext to translate server-side static strings
|
|
2895
|
-
* using translations.json and the active Polylang language.
|
|
2896
|
-
*/
|
|
2897
|
-
function forgewp_gettext_translation_bridge($translated, $text, $domain) {
|
|
2898
|
-
if ($domain === '${config.textDomain}') {
|
|
2899
|
-
static $translations = null;
|
|
2900
|
-
if ($translations === null) {
|
|
2901
|
-
$file = get_template_directory() . '/translations.json';
|
|
2902
|
-
if (file_exists($file)) {
|
|
2903
|
-
$translations = json_decode(file_get_contents($file), true);
|
|
2904
|
-
} else {
|
|
2905
|
-
$translations = array();
|
|
2906
|
-
}
|
|
2907
|
-
}
|
|
2908
|
-
$type = '';
|
|
2909
|
-
if (function_exists('pll_current_language')) {
|
|
2910
|
-
$type = pll_current_language();
|
|
2911
|
-
}
|
|
2912
|
-
if (empty($type)) {
|
|
2913
|
-
$type = get_locale();
|
|
2914
|
-
}
|
|
2915
|
-
$type = substr($type, 0, 2);
|
|
2916
|
-
if (is_array($translations) && isset($translations[$type][$text])) {
|
|
2917
|
-
return $translations[$type][$text];
|
|
2918
|
-
}
|
|
2919
|
-
}
|
|
2920
|
-
return $translated;
|
|
2921
|
-
}
|
|
2922
|
-
add_filter('gettext', 'forgewp_gettext_translation_bridge', 10, 3);
|
|
2923
|
-
add_filter('ngettext', 'forgewp_gettext_translation_bridge', 10, 3);
|
|
2924
|
-
|
|
2925
|
-
/**
|
|
2926
|
-
* Dynamic SVG theme icon rendering callback.
|
|
2927
|
-
* Maps dynamic icon slugs to inline SVGs from Lucide or custom developer uploads.
|
|
2928
|
-
*/
|
|
2929
|
-
function forgewp_render_theme_icon($icon_slug, $class_name = '', $provider = 'lucide') {
|
|
2930
|
-
$svgs = array(
|
|
2931
|
-
'award' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-award"><path d="m15.477 12.89 1.515 8.526a.5.5 0 0 1-.81.47l-3.58-2.687a1 1 0 0 0-1.197 0l-3.586 2.686a.5.5 0 0 1-.81-.469l1.514-8.526"></path><circle cx="12" cy="8" r="6"></circle></svg>',
|
|
2932
|
-
'shield' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-shield"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"></path></svg>',
|
|
2933
|
-
'globe' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-globe"><circle cx="12" cy="12" r="10"></circle><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"></path><path d="M2 12h20"></path></svg>',
|
|
2934
|
-
'users' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-users"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"></path><circle cx="9" cy="7" r="4"></circle><path d="M22 21v-2a4 4 0 0 0-3-3.87"></path><path d="M16 3.13a4 4 0 0 1 0 7.75"></path></svg>',
|
|
2935
|
-
);
|
|
2936
|
-
|
|
2937
|
-
$custom_svgs = get_option(\'forgewp_custom_icons\', array());
|
|
2938
|
-
if ($provider === \'custom\' && isset($custom_svgs[$icon_slug])) {
|
|
2939
|
-
$svg = $custom_svgs[$icon_slug];
|
|
2940
|
-
} else {
|
|
2941
|
-
$svg = isset($svgs[$icon_slug]) ? $svgs[$icon_slug] : '';
|
|
2942
|
-
}
|
|
2943
|
-
|
|
2944
|
-
if (!empty($svg)) {
|
|
2945
|
-
if (!empty($class_name)) {
|
|
2946
|
-
$svg = str_replace('<svg ', '<svg class="' . esc_attr($class_name) . '" ', $svg);
|
|
2947
|
-
}
|
|
2948
|
-
echo $svg;
|
|
2949
|
-
}
|
|
2950
|
-
}
|
|
2951
|
-
${faviconPhp}${sitemapsPhp}${queryEndpointsPhp}${authControllersPhp}
|
|
2952
|
-
|
|
2953
|
-
/**
|
|
2954
|
-
* Prevent TinyMCE Classic Editor auto-resize height expansion feedback loop.
|
|
2955
|
-
*/
|
|
2956
|
-
function forgewp_disable_editor_autoresize($init_array) {
|
|
2957
|
-
if (isset($init_array['plugins'])) {
|
|
2958
|
-
$plugins = explode(',', $init_array['plugins']);
|
|
2959
|
-
$key = array_search('wpautoresize', $plugins);
|
|
2960
|
-
if ($key !== false) {
|
|
2961
|
-
unset($plugins[$key]);
|
|
2962
|
-
$init_array['plugins'] = implode(',', $plugins);
|
|
2963
|
-
}
|
|
2964
|
-
}
|
|
2965
|
-
return $init_array;
|
|
2966
|
-
}
|
|
2967
|
-
add_filter('tiny_mce_before_init', 'forgewp_disable_editor_autoresize');
|
|
2968
|
-
`;
|
|
2969
|
-
}
|
|
2970
|
-
|