@@ -193,16 +193,6 @@ public function create( $_, $assoc_args ) {
193
193
}
194
194
}
195
195
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 );
206
196
if ( empty ( $ assoc_args ['dbprefix ' ] ) ) {
207
197
WP_CLI ::error ( '--dbprefix cannot be empty ' );
208
198
}
@@ -239,52 +229,126 @@ public function create( $_, $assoc_args ) {
239
229
// phpcs:enable WordPress.DB.RestrictedFunctions
240
230
}
241
231
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
+
242
248
if ( ! Utils \get_flag_value ( $ assoc_args , 'skip-salts ' ) ) {
243
249
try {
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 ();
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 ();
254
260
} catch ( Exception $ e ) {
255
- $ assoc_args ['keys-and-salts ' ] = false ;
256
- $ assoc_args ['keys-and-salts-alt ' ] = self ::fetch_remote_salts (
261
+ $ defaults ['keys-and-salts ' ] = false ;
262
+ $ defaults ['keys-and-salts-alt ' ] = self ::fetch_remote_salts (
257
263
(bool ) Utils \get_flag_value ( $ assoc_args , 'insecure ' , false )
258
264
);
259
265
}
260
266
}
261
267
262
- if ( Utils \wp_version_compare ( '4.0 ' , '< ' ) ) {
263
- $ assoc_args ['add-wplang ' ] = true ;
264
- } else {
265
- $ assoc_args ['add-wplang ' ] = false ;
266
- }
267
-
268
- foreach ( $ assoc_args as $ key => $ value ) {
269
- $ assoc_args [ $ key ] = $ this ->escape_config_value ( $ key , $ value );
268
+ $ path = $ defaults ['config-file ' ];
269
+ if ( ! empty ( $ assoc_args ['config-file ' ] ) ) {
270
+ $ path = $ assoc_args ['config-file ' ];
270
271
}
271
272
272
- // 'extra-php' from STDIN is retrieved after escaping to avoid breaking
273
- // the PHP code.
274
273
if ( Utils \get_flag_value ( $ assoc_args , 'extra-php ' ) === true ) {
275
- $ assoc_args ['extra-php ' ] = file_get_contents ( 'php://stdin ' );
274
+ // 'extra-php' from STDIN is retrieved.
275
+ $ defaults ['extra-php ' ] = file_get_contents ( 'php://stdin ' );
276
276
}
277
277
278
278
$ command_root = Utils \phar_safe_path ( dirname ( __DIR__ ) );
279
- $ out = Utils \mustache_render ( "{$ command_root }/templates/wp-config.mustache " , $ assoc_args );
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 );
280
284
281
- $ wp_config_file_name = basename ( $ assoc_args ['config-file ' ] );
282
- $ bytes_written = file_put_contents ( $ assoc_args ['config-file ' ], $ out );
283
285
if ( ! $ bytes_written ) {
284
286
WP_CLI ::error ( "Could not create new ' {$ wp_config_file_name }' file. " );
285
- } else {
286
- WP_CLI ::success ( "Generated ' {$ wp_config_file_name }' file. " );
287
287
}
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. " );
288
352
}
289
353
290
354
/**
0 commit comments