Skip to content

Commit ae65f96

Browse files
committed
Allow configuring default constants in WP config
1 parent e79d732 commit ae65f96

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export class BlueprintsV1Handler {
146146
mountsBeforeWpInstall,
147147
mountsAfterWpInstall,
148148
wordPressZip: wordPressZip && (await wordPressZip!.arrayBuffer()),
149+
wpConfigDefaultConstants: this.args.wpConfigDefaultConstants,
149150
sqliteIntegrationPluginZip:
150151
await sqliteIntegrationPluginZip?.arrayBuffer(),
151152
firstProcessId: 0,

packages/playground/cli/src/blueprints-v1/worker-thread-v1.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type WorkerBootOptions = {
4949
export type PrimaryWorkerBootOptions = WorkerBootOptions & {
5050
wpVersion?: string;
5151
wordPressZip?: ArrayBuffer;
52+
wpConfigDefaultConstants?: Record<string, string | number | boolean | null>;
5253
sqliteIntegrationPluginZip?: ArrayBuffer;
5354
dataSqlPath?: string;
5455
};
@@ -128,6 +129,7 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker {
128129
mountsAfterWpInstall,
129130
phpVersion: php = RecommendedPHPVersion,
130131
wordPressZip,
132+
wpConfigDefaultConstants,
131133
sqliteIntegrationPluginZip,
132134
firstProcessId,
133135
processIdSpaceLength,
@@ -181,6 +183,7 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker {
181183
wordPressZip !== undefined
182184
? new File([wordPressZip], 'wordpress.zip')
183185
: undefined,
186+
wpConfigDefaultConstants,
184187
sqliteIntegrationPluginZip:
185188
sqliteIntegrationPluginZip !== undefined
186189
? new File(

packages/playground/cli/src/blueprints-v2/worker-thread-v2.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export type WorkerBootRequestHandlerOptions = Omit<
152152
'mountsBeforeWpInstall' | 'mountsAfterWpInstall'
153153
> & {
154154
onPHPInstanceCreated: PHPInstanceCreatedHook;
155+
wpConfigDefaultConstants?: Record<string, string | number | boolean | null>;
155156
};
156157

157158
export class PlaygroundCliBlueprintV2Worker extends PHPWorker {
@@ -232,6 +233,7 @@ export class PlaygroundCliBlueprintV2Worker extends PHPWorker {
232233
});
233234
}
234235
},
236+
wpConfigDefaultConstants: args.wpConfigDefaultConstants,
235237
};
236238
await this.bootRequestHandler(requestHandlerOptions);
237239

@@ -415,6 +417,7 @@ export class PlaygroundCliBlueprintV2Worker extends PHPWorker {
415417
nativeInternalDirPath,
416418
withXdebug,
417419
onPHPInstanceCreated,
420+
wpConfigDefaultConstants,
418421
}: WorkerBootRequestHandlerOptions) {
419422
if (this.booted) {
420423
throw new Error('Playground already booted');
@@ -458,6 +461,7 @@ export class PlaygroundCliBlueprintV2Worker extends PHPWorker {
458461
phpIniEntries,
459462
cookieStore: false,
460463
spawnHandler: sandboxedSpawnHandlerFactory,
464+
wpConfigDefaultConstants,
461465
});
462466
this.__internal_setRequestHandler(requestHandler);
463467

packages/playground/cli/src/run-cli.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ export async function parseOptionsAndRunCLI() {
152152
type: 'boolean',
153153
default: false,
154154
})
155+
.option('wp-config-default-constants', {
156+
describe:
157+
'Configure default constant values to use in "wp-config.php", in case the constants are missing. Encoded as JSON string.',
158+
type: 'string',
159+
coerce: (value: string) => {
160+
try {
161+
return JSON.parse(value);
162+
} catch (e) {
163+
throw new Error(
164+
'Invalid JSON string for --wp-config-default-constants'
165+
);
166+
}
167+
},
168+
})
155169
.option('skip-wordpress-setup', {
156170
describe:
157171
'Do not download, unzip, and install WordPress. Useful for mounting a pre-configured WordPress directory at /wordpress.',
@@ -416,6 +430,7 @@ export interface RunCLIArgs {
416430
xdebug?: boolean;
417431
experimentalDevtools?: boolean;
418432
'experimental-blueprints-v2-runner'?: boolean;
433+
wpConfigDefaultConstants?: Record<string, string | number | boolean | null>;
419434

420435
// --------- Blueprint V1 args -----------
421436
skipWordPressSetup?: boolean;

packages/playground/wordpress/src/boot.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ export interface BootWordPressOptions {
126126
dataSqlPath?: string;
127127
/** Zip with the WordPress installation to extract in /wordpress. */
128128
wordPressZip?: File | Promise<File> | undefined;
129+
/**
130+
* Default constant values to use in "wp-config.php", in case they are missing.
131+
*/
132+
wpConfigDefaultConstants?: Record<string, string | number | boolean | null>;
129133
/** Preloaded SQLite integration plugin. */
130134
sqliteIntegrationPluginZip?: File | Promise<File>;
131135
/**
@@ -186,7 +190,11 @@ export async function bootWordPress(
186190
* This is needed, because some WordPress backups and exports may not include
187191
* definitions for some of the necessary constants.
188192
*/
189-
await ensureWpConfig(php, requestHandler.documentRoot);
193+
await ensureWpConfig(
194+
php,
195+
requestHandler.documentRoot,
196+
options.wpConfigDefaultConstants
197+
);
190198
// Run "before database" hooks to mount/copy more files in
191199
if (options.hooks?.beforeDatabaseSetup) {
192200
await options.hooks.beforeDatabaseSetup(php);

packages/playground/wordpress/src/test/wp-config.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,40 @@ if ( ! defined( 'ABSPATH' ) ) {
246246
require_once ABSPATH . 'wp-settings.php';
247247
`);
248248
});
249+
250+
it('should allow configuring default constants', async () => {
251+
php.writeFile(
252+
wpConfigPath,
253+
`<?php
254+
echo json_encode([
255+
'DB_NAME' => DB_NAME,
256+
'DB_USER' => DB_USER,
257+
'CUSTOM_CONSTANT' => CUSTOM_CONSTANT,
258+
'WP_DEBUG' => WP_DEBUG,
259+
]);`
260+
);
261+
262+
await ensureWpConfig(php, documentRoot, {
263+
DB_NAME: 'custom-name',
264+
CUSTOM_CONSTANT: 'custom-value',
265+
});
266+
267+
const rewritten = php.readFileAsText(wpConfigPath);
268+
expect(rewritten).toContain(`define( 'DB_NAME', 'custom-name' );`);
269+
expect(rewritten).toContain(`define( 'DB_USER', 'wordpress' );`);
270+
expect(rewritten).toContain(
271+
`define( 'CUSTOM_CONSTANT', 'custom-value' );`
272+
);
273+
expect(rewritten).toContain(`define( 'WP_DEBUG', false );`);
274+
275+
const response = await php.run({ code: rewritten });
276+
expect(response.json).toEqual({
277+
DB_NAME: 'custom-name',
278+
DB_USER: 'wordpress',
279+
CUSTOM_CONSTANT: 'custom-value',
280+
WP_DEBUG: false,
281+
});
282+
});
249283
});
250284

251285
describe('defineWpConfigConstants', () => {

packages/playground/wordpress/src/wp-config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import wpConfigTransformer from './wp-config-transformer.php?raw';
1414
*/
1515
export async function ensureWpConfig(
1616
php: UniversalPHP,
17-
documentRoot: string
17+
documentRoot: string,
18+
defaultConstants: Record<string, string | number | boolean | null> = {}
1819
): Promise<void> {
1920
const wpConfigPath = joinPaths(documentRoot, 'wp-config.php');
2021

@@ -35,6 +36,7 @@ export async function ensureWpConfig(
3536
LOGGED_IN_SALT: 'put your unique phrase here',
3637
NONCE_SALT: 'put your unique phrase here',
3738
WP_DEBUG: false,
39+
...defaultConstants,
3840
};
3941

4042
/**

0 commit comments

Comments
 (0)