diff --git a/apps/cli/lib/native-php/site-setup.ts b/apps/cli/lib/native-php/site-setup.ts index 138ccdc604..87232cadd2 100644 --- a/apps/cli/lib/native-php/site-setup.ts +++ b/apps/cli/lib/native-php/site-setup.ts @@ -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( @@ -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 ); `; @@ -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 } ); diff --git a/apps/cli/lib/native-php/tests/site-setup.test.ts b/apps/cli/lib/native-php/tests/site-setup.test.ts index 1d202a9c8c..ccad856fc7 100644 --- a/apps/cli/lib/native-php/tests/site-setup.test.ts +++ b/apps/cli/lib/native-php/tests/site-setup.test.ts @@ -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', () => { @@ -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 } );