@@ -193,6 +193,16 @@ public function create( $_, $assoc_args ) {
193193 }
194194 }
195195
196+ $ defaults = [
197+ 'dbhost ' => 'localhost ' ,
198+ 'dbpass ' => '' ,
199+ 'dbprefix ' => 'wp_ ' ,
200+ 'dbcharset ' => 'utf8 ' ,
201+ 'dbcollate ' => '' ,
202+ 'locale ' => self ::get_initial_locale (),
203+ 'config-file ' => rtrim ( ABSPATH , '/ \\' ) . '/wp-config.php ' ,
204+ ];
205+ $ assoc_args = array_merge ( $ defaults , $ assoc_args );
196206 if ( empty ( $ assoc_args ['dbprefix ' ] ) ) {
197207 WP_CLI ::error ( '--dbprefix cannot be empty ' );
198208 }
@@ -229,126 +239,52 @@ public function create( $_, $assoc_args ) {
229239 // phpcs:enable WordPress.DB.RestrictedFunctions
230240 }
231241
232- $ defaults = [
233- 'dbhost ' => 'localhost ' ,
234- 'dbpass ' => '' ,
235- 'dbprefix ' => 'wp_ ' ,
236- 'dbcharset ' => 'utf8 ' ,
237- 'dbcollate ' => '' ,
238- 'locale ' => self ::get_initial_locale (),
239- 'config-file ' => rtrim ( ABSPATH , '/ \\' ) . '/wp-config.php ' ,
240- ];
241-
242- if ( Utils \wp_version_compare ( '4.0 ' , '< ' ) ) {
243- $ defaults ['add-wplang ' ] = true ;
244- } else {
245- $ defaults ['add-wplang ' ] = false ;
246- }
247-
248242 if ( ! Utils \get_flag_value ( $ assoc_args , 'skip-salts ' ) ) {
249243 try {
250- $ defaults ['keys-and-salts ' ] = true ;
251- $ defaults ['auth-key ' ] = self ::unique_key ();
252- $ defaults ['secure-auth-key ' ] = self ::unique_key ();
253- $ defaults ['logged-in-key ' ] = self ::unique_key ();
254- $ defaults ['nonce-key ' ] = self ::unique_key ();
255- $ defaults ['auth-salt ' ] = self ::unique_key ();
256- $ defaults ['secure-auth-salt ' ] = self ::unique_key ();
257- $ defaults ['logged-in-salt ' ] = self ::unique_key ();
258- $ defaults ['nonce-salt ' ] = self ::unique_key ();
259- $ defaults ['wp-cache-key-salt ' ] = self ::unique_key ();
244+ $ assoc_args ['keys-and-salts ' ] = true ;
245+ $ assoc_args ['auth-key ' ] = self ::unique_key ();
246+ $ assoc_args ['secure-auth-key ' ] = self ::unique_key ();
247+ $ assoc_args ['logged-in-key ' ] = self ::unique_key ();
248+ $ assoc_args ['nonce-key ' ] = self ::unique_key ();
249+ $ assoc_args ['auth-salt ' ] = self ::unique_key ();
250+ $ assoc_args ['secure-auth-salt ' ] = self ::unique_key ();
251+ $ assoc_args ['logged-in-salt ' ] = self ::unique_key ();
252+ $ assoc_args ['nonce-salt ' ] = self ::unique_key ();
253+ $ assoc_args ['wp-cache-key-salt ' ] = self ::unique_key ();
260254 } catch ( Exception $ e ) {
261- $ defaults ['keys-and-salts ' ] = false ;
262- $ defaults ['keys-and-salts-alt ' ] = self ::fetch_remote_salts (
255+ $ assoc_args ['keys-and-salts ' ] = false ;
256+ $ assoc_args ['keys-and-salts-alt ' ] = self ::fetch_remote_salts (
263257 (bool ) Utils \get_flag_value ( $ assoc_args , 'insecure ' , false )
264258 );
265259 }
266260 }
267261
268- $ path = $ defaults ['config-file ' ];
269- if ( ! empty ( $ assoc_args ['config-file ' ] ) ) {
270- $ path = $ assoc_args ['config-file ' ];
262+ if ( Utils \wp_version_compare ( '4.0 ' , '< ' ) ) {
263+ $ assoc_args ['add-wplang ' ] = true ;
264+ } else {
265+ $ assoc_args ['add-wplang ' ] = false ;
271266 }
272267
268+ foreach ( $ assoc_args as $ key => $ value ) {
269+ $ assoc_args [ $ key ] = $ this ->escape_config_value ( $ key , $ value );
270+ }
271+
272+ // 'extra-php' from STDIN is retrieved after escaping to avoid breaking
273+ // the PHP code.
273274 if ( Utils \get_flag_value ( $ assoc_args , 'extra-php ' ) === true ) {
274- // 'extra-php' from STDIN is retrieved.
275- $ defaults ['extra-php ' ] = file_get_contents ( 'php://stdin ' );
275+ $ assoc_args ['extra-php ' ] = file_get_contents ( 'php://stdin ' );
276276 }
277277
278278 $ command_root = Utils \phar_safe_path ( dirname ( __DIR__ ) );
279- $ out = Utils \mustache_render ( "{$ command_root }/templates/wp-config.mustache " , $ defaults );
280-
281- // Output the default config file at path specified in assoc args.
282- $ wp_config_file_name = basename ( $ path );
283- $ bytes_written = file_put_contents ( $ path , $ out );
279+ $ out = Utils \mustache_render ( "{$ command_root }/templates/wp-config.mustache " , $ assoc_args );
284280
281+ $ wp_config_file_name = basename ( $ assoc_args ['config-file ' ] );
282+ $ bytes_written = file_put_contents ( $ assoc_args ['config-file ' ], $ out );
285283 if ( ! $ bytes_written ) {
286284 WP_CLI ::error ( "Could not create new ' {$ wp_config_file_name }' file. " );
285+ } else {
286+ WP_CLI ::success ( "Generated ' {$ wp_config_file_name }' file. " );
287287 }
288-
289- $ assoc_args = array_merge ( $ defaults , $ assoc_args );
290-
291- $ options = [
292- 'raw ' => false ,
293- 'add ' => true ,
294- 'normalize ' => true ,
295- ];
296-
297- $ config_keys = [
298- 'dbhost ' => array (
299- 'name ' => 'DB_HOST ' ,
300- 'type ' => 'constant ' ,
301- ),
302- 'dbpass ' => array (
303- 'name ' => 'DB_PASSWORD ' ,
304- 'type ' => 'constant ' ,
305- ),
306- 'dbprefix ' => array (
307- 'name ' => 'table_prefix ' ,
308- 'type ' => 'variable ' ,
309- ),
310- 'dbcharset ' => array (
311- 'name ' => 'DB_CHARSET ' ,
312- 'type ' => 'constant ' ,
313- ),
314- 'dbcollate ' => array (
315- 'name ' => 'DB_COLLATE ' ,
316- 'type ' => 'constant ' ,
317- ),
318- 'locale ' => array (
319- 'name ' => 'WPLANG ' ,
320- 'type ' => 'constant ' ,
321- ),
322- 'dbname ' => array (
323- 'name ' => 'DB_NAME ' ,
324- 'type ' => 'constant ' ,
325- ),
326- 'dbuser ' => array (
327- 'name ' => 'DB_USER ' ,
328- 'type ' => 'constant ' ,
329- ),
330- ];
331-
332- try {
333- $ config_transformer = new WPConfigTransformer ( $ path );
334-
335- foreach ( $ config_keys as $ key => $ const ) {
336-
337- $ value = $ assoc_args [ $ key ];
338- if ( ! empty ( $ value ) ) {
339- $ config_transformer ->update ( $ const ['type ' ], $ const ['name ' ], $ value , $ options );
340- }
341- }
342- } catch ( Exception $ exception ) {
343- // Remove the default moustache wp-config.php template file.
344- if ( file_exists ( $ assoc_args ['config-file ' ] ) ) {
345- unlink ( $ path );
346- }
347-
348- WP_CLI ::error ( "Could not create new ' {$ wp_config_file_name }' file. \nReason: {$ exception ->getMessage ()}" );
349- }
350-
351- WP_CLI ::success ( "Generated ' {$ wp_config_file_name }' file. " );
352288 }
353289
354290 /**
0 commit comments