@@ -200,6 +200,8 @@ private static function get_initial_locale() {
200200 * Success: Generated 'wp-config.php' file.
201201 */
202202 public function create ( $ _ , $ assoc_args ) {
203+ $ provided_assoc_args = $ assoc_args ;
204+
203205 if ( ! Utils \get_flag_value ( $ assoc_args , 'force ' ) ) {
204206 if ( isset ( $ assoc_args ['config-file ' ] ) && file_exists ( $ assoc_args ['config-file ' ] ) ) {
205207 $ this ->config_file_already_exist_error ( basename ( $ assoc_args ['config-file ' ] ) );
@@ -304,26 +306,156 @@ public function create( $_, $assoc_args ) {
304306 }
305307 }
306308
307- foreach ( $ assoc_args as $ key => $ value ) {
308- $ assoc_args [ $ key ] = $ this ->escape_config_value ( $ key , $ value );
309+ $ template_args = array_merge (
310+ $ defaults ,
311+ [
312+ 'keys-and-salts ' => false ,
313+ 'keys-and-salts-alt ' => '' ,
314+ 'extra-php ' => '' ,
315+ ]
316+ );
317+
318+ if ( ! empty ( $ assoc_args ['keys-and-salts ' ] ) ) {
319+ $ template_args ['keys-and-salts ' ] = true ;
320+ $ template_args ['auth-key ' ] = '' ;
321+ $ template_args ['secure-auth-key ' ] = '' ;
322+ $ template_args ['logged-in-key ' ] = '' ;
323+ $ template_args ['nonce-key ' ] = '' ;
324+ $ template_args ['auth-salt ' ] = '' ;
325+ $ template_args ['secure-auth-salt ' ] = '' ;
326+ $ template_args ['logged-in-salt ' ] = '' ;
327+ $ template_args ['nonce-salt ' ] = '' ;
328+ $ template_args ['wp-cache-key-salt ' ] = '' ;
329+ } elseif ( ! empty ( $ assoc_args ['keys-and-salts-alt ' ] ) ) {
330+ $ template_args ['keys-and-salts-alt ' ] = $ assoc_args ['keys-and-salts-alt ' ];
309331 }
310332
311- // 'extra-php' from STDIN is retrieved after escaping to avoid breaking
312- // the PHP code.
313- if ( Utils \get_flag_value ( $ assoc_args , 'extra-php ' ) === true ) {
314- $ assoc_args ['extra-php ' ] = file_get_contents ( 'php://stdin ' );
333+ $ extra_php = Utils \get_flag_value ( $ assoc_args , 'extra-php ' );
334+ if ( true === $ extra_php ) {
335+ $ template_args ['extra-php ' ] = file_get_contents ( 'php://stdin ' );
336+ } elseif ( is_string ( $ extra_php ) ) {
337+ $ template_args ['extra-php ' ] = $ extra_php ;
315338 }
316339
317340 $ command_root = Path ::phar_safe ( dirname ( __DIR__ ) );
318- $ out = Utils \mustache_render ( "{$ command_root }/templates/wp-config.mustache " , $ assoc_args );
341+ $ out = Utils \mustache_render ( "{$ command_root }/templates/wp-config.mustache " , $ template_args );
319342
320- $ wp_config_file_name = basename ( $ assoc_args ['config-file ' ] );
321- $ bytes_written = file_put_contents ( $ assoc_args ['config-file ' ], $ out );
343+ $ wp_config_file_name = basename ( $ assoc_args ['config-file ' ] );
344+ $ created_config_file = ! file_exists ( $ assoc_args ['config-file ' ] );
345+ $ original_config_contents = $ created_config_file ? null : file_get_contents ( $ assoc_args ['config-file ' ] );
346+ if ( false === $ original_config_contents ) {
347+ $ original_config_contents = null ;
348+ }
349+ $ bytes_written = file_put_contents ( $ assoc_args ['config-file ' ], $ out );
322350 if ( ! $ bytes_written ) {
323351 WP_CLI ::error ( "Could not create new ' {$ wp_config_file_name }' file. " );
324- } else {
325- WP_CLI ::success ( "Generated ' {$ wp_config_file_name }' file. " );
326352 }
353+
354+ try {
355+ $ config_transformer = new WPConfigTransformer ( $ assoc_args ['config-file ' ] );
356+
357+ $ value_map = [
358+ 'dbname ' => [
359+ 'type ' => 'constant ' ,
360+ 'name ' => 'DB_NAME ' ,
361+ 'anchor ' => '/** Database username */ ' ,
362+ ],
363+ 'dbuser ' => [
364+ 'type ' => 'constant ' ,
365+ 'name ' => 'DB_USER ' ,
366+ 'anchor ' => '/** Database password */ ' ,
367+ ],
368+ 'dbpass ' => [
369+ 'type ' => 'constant ' ,
370+ 'name ' => 'DB_PASSWORD ' ,
371+ 'anchor ' => '/** Database hostname */ ' ,
372+ ],
373+ 'dbhost ' => [
374+ 'type ' => 'constant ' ,
375+ 'name ' => 'DB_HOST ' ,
376+ 'anchor ' => '/** Database charset to use in creating database tables. */ ' ,
377+ ],
378+ 'dbcharset ' => [
379+ 'type ' => 'constant ' ,
380+ 'name ' => 'DB_CHARSET ' ,
381+ 'anchor ' => '/** The database collate type. Don \'t change this if in doubt. */ ' ,
382+ ],
383+ 'dbcollate ' => [
384+ 'type ' => 'constant ' ,
385+ 'name ' => 'DB_COLLATE ' ,
386+ 'anchor ' => '/**#@+ ' ,
387+ ],
388+ 'dbprefix ' => [
389+ 'type ' => 'variable ' ,
390+ 'name ' => 'table_prefix ' ,
391+ 'anchor ' => '/* Add any custom values between this line and the "stop editing" line. */ ' ,
392+ ],
393+ ];
394+
395+ foreach ( $ value_map as $ arg_name => $ entry ) {
396+ if ( ! array_key_exists ( $ arg_name , $ provided_assoc_args ) ) {
397+ continue ;
398+ }
399+
400+ if ( is_string ( $ provided_assoc_args [ $ arg_name ] ) && false !== strpos ( $ provided_assoc_args [ $ arg_name ], '\\' ) ) {
401+ // Use remove+update to preserve backslash escaping while keeping insertion behavior aligned with the rest of this class.
402+ $ config_transformer ->remove ( $ entry ['type ' ], $ entry ['name ' ] );
403+ $ config_transformer ->update (
404+ $ entry ['type ' ],
405+ $ entry ['name ' ],
406+ $ provided_assoc_args [ $ arg_name ],
407+ [
408+ 'add ' => true ,
409+ 'anchor ' => $ entry ['anchor ' ],
410+ 'placement ' => 'before ' ,
411+ ]
412+ );
413+ continue ;
414+ }
415+
416+ $ config_transformer ->update (
417+ $ entry ['type ' ],
418+ $ entry ['name ' ],
419+ $ provided_assoc_args [ $ arg_name ],
420+ [ 'add ' => true ]
421+ );
422+ }
423+
424+ if ( ! empty ( $ assoc_args ['keys-and-salts ' ] ) ) {
425+ $ salt_map = [
426+ 'AUTH_KEY ' => 'auth-key ' ,
427+ 'SECURE_AUTH_KEY ' => 'secure-auth-key ' ,
428+ 'LOGGED_IN_KEY ' => 'logged-in-key ' ,
429+ 'NONCE_KEY ' => 'nonce-key ' ,
430+ 'AUTH_SALT ' => 'auth-salt ' ,
431+ 'SECURE_AUTH_SALT ' => 'secure-auth-salt ' ,
432+ 'LOGGED_IN_SALT ' => 'logged-in-salt ' ,
433+ 'NONCE_SALT ' => 'nonce-salt ' ,
434+ 'WP_CACHE_KEY_SALT ' => 'wp-cache-key-salt ' ,
435+ ];
436+
437+ foreach ( $ salt_map as $ name => $ arg_name ) {
438+ if ( ! array_key_exists ( $ arg_name , $ assoc_args ) ) {
439+ continue ;
440+ }
441+ $ config_transformer ->update ( 'constant ' , $ name , $ assoc_args [ $ arg_name ], [ 'add ' => true ] );
442+ }
443+ }
444+ } catch ( Throwable $ exception ) {
445+ $ cleanup_error = '' ;
446+ if ( $ created_config_file && file_exists ( $ assoc_args ['config-file ' ] ) ) {
447+ if ( ! unlink ( $ assoc_args ['config-file ' ] ) ) {
448+ $ cleanup_error = "\nCleanup: Could not remove ' {$ wp_config_file_name }' after failure. " ;
449+ }
450+ } elseif ( null !== $ original_config_contents ) {
451+ if ( false === file_put_contents ( $ assoc_args ['config-file ' ], $ original_config_contents ) ) {
452+ $ cleanup_error = "\nCleanup: Could not restore the original ' {$ wp_config_file_name }' after failure. " ;
453+ }
454+ }
455+ WP_CLI ::error ( "Could not process the ' {$ wp_config_file_name }' transformation. \nReason: {$ exception ->getMessage ()}{$ cleanup_error }" );
456+ }
457+
458+ WP_CLI ::success ( "Generated ' {$ wp_config_file_name }' file. " );
327459 }
328460
329461 /**
@@ -1521,31 +1653,4 @@ private static function is_sqlite_integration_active() {
15211653 $ db_dropin_contents
15221654 );
15231655 }
1524-
1525- /**
1526- * Escape a config value so it can be safely used within single quotes.
1527- *
1528- * @param string $key Key into the arguments array.
1529- * @param mixed $value Value to escape.
1530- * @return mixed Escaped value.
1531- */
1532- private function escape_config_value ( $ key , $ value ) {
1533- // Skip 'extra-php', it mustn't be escaped.
1534- if ( 'extra-php ' === $ key ) {
1535- return $ value ;
1536- }
1537-
1538- // Skip 'keys-and-salts-alt' and assume they are safe.
1539- if ( 'keys-and-salts-alt ' === $ key && ! empty ( $ value ) ) {
1540- return $ value ;
1541- }
1542-
1543- if ( is_string ( $ value ) ) {
1544- $ value = str_replace ( '\\' , '\\\\' , $ value ); // Escape backslashes first
1545- $ value = str_replace ( "' " , "\\' " , $ value ); // Then escape single quotes
1546- return $ value ;
1547- }
1548-
1549- return $ value ;
1550- }
15511656}
0 commit comments