Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/cli/lib/native-php/site-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const WP_CONFIG_TRANSFORMER_PATH = path.resolve(

const DEFAULT_WP_CONFIG_CONSTANTS = { DB_NAME: 'wordpress' } as const;

// Defined only when the site has not set them itself. Theme development mode
// makes new theme pattern files and theme.json edits apply on the next load
// instead of waiting out core's caches.
const SITE_DEFAULT_WP_CONFIG_CONSTANTS = { WP_DEVELOPMENT_MODE: 'theme' } as const;

type Logger = ( ...args: Parameters< typeof console.log > ) => void;

export async function ensureWpConfig(
Expand All @@ -45,6 +50,11 @@ require_once $transformer_path;

$transformer = WP_Config_Transformer::from_file( $wp_config_path );
$transformer->define_constants( $constants );
foreach ( json_decode( $argv[4] ?? '', true ) ?: array() as $name => $value ) {
if ( ! $transformer->constant_exists( $name ) ) {
$transformer->define_constant( $name, $value );
}
}
$transformer->to_file( $wp_config_path );
`;

Expand Down Expand Up @@ -74,6 +84,7 @@ $transformer->to_file( $wp_config_path );
WP_CONFIG_TRANSFORMER_PATH,
wpConfigPath,
JSON.stringify( constants ),
JSON.stringify( SITE_DEFAULT_WP_CONFIG_CONSTANTS ),
],
{ phpVersion, signal }
);
Expand Down
10 changes: 9 additions & 1 deletion apps/cli/lib/native-php/tests/site-setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function getWrittenConstants(
runPhpCommand.mockClear();
await ensureWpConfig( '/nonexistent-site', '8.4', new AbortController().signal, config );
const args = runPhpCommand.mock.calls[ 0 ][ 0 ] as string[];
return JSON.parse( args[ args.length - 1 ] );
return JSON.parse( args[ args.length - 2 ] );
}

describe( 'ensureWpConfig', () => {
Expand All @@ -33,6 +33,14 @@ describe( 'ensureWpConfig', () => {
expect( constants.WP_ENVIRONMENT_TYPE ).toBe( 'local' );
} );

it( 'defaults theme development mode without replacing a site’s own', async () => {
runPhpCommand.mockClear();
await ensureWpConfig( '/nonexistent-site', '8.4', new AbortController().signal );
const args = runPhpCommand.mock.calls[ 0 ][ 0 ] as string[];
expect( JSON.parse( args[ args.length - 1 ] ) ).toEqual( { WP_DEVELOPMENT_MODE: 'theme' } );
expect( args[ 1 ] ).toContain( 'constant_exists( $name )' );
} );

it( 'writes SCRIPT_DEBUG when script debug is enabled', async () => {
const constants = await getWrittenConstants( { enableScriptDebug: true } );

Expand Down
Loading