Skip to content

Commit a4ae2c7

Browse files
authored
Merge pull request #181 from UmeshSingla/94-refactor-config-command
2 parents 445dfd0 + 0601669 commit a4ae2c7

File tree

2 files changed

+111
-38
lines changed

2 files changed

+111
-38
lines changed

features/config-create.feature

+9
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,12 @@ Feature: Create a wp-config file
293293
PasswordWith'SingleQuotes'
294294
"""
295295
296+
Scenario: Correct config file is generated when database password has double quote in it
297+
Given an empty directory
298+
And WP files
299+
300+
When I run `wp config create --skip-check --dbname=somedb --dbuser=someuser --dbpass='p@(ss){w0r?d><}"!With"DoubleQuotes'`
301+
Then the wp-config.php file should contain:
302+
"""
303+
define( 'DB_PASSWORD', 'p@(ss){w0r?d><}"!With"DoubleQuotes' )
304+
"""

src/Config_Command.php

+102-38
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,6 @@ 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 );
206196
if ( empty( $assoc_args['dbprefix'] ) ) {
207197
WP_CLI::error( '--dbprefix cannot be empty' );
208198
}
@@ -239,52 +229,126 @@ public function create( $_, $assoc_args ) {
239229
// phpcs:enable WordPress.DB.RestrictedFunctions
240230
}
241231

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+
242248
if ( ! Utils\get_flag_value( $assoc_args, 'skip-salts' ) ) {
243249
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();
254260
} 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(
257263
(bool) Utils\get_flag_value( $assoc_args, 'insecure', false )
258264
);
259265
}
260266
}
261267

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'];
270271
}
271272

272-
// 'extra-php' from STDIN is retrieved after escaping to avoid breaking
273-
// the PHP code.
274273
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' );
276276
}
277277

278278
$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 );
280284

281-
$wp_config_file_name = basename( $assoc_args['config-file'] );
282-
$bytes_written = file_put_contents( $assoc_args['config-file'], $out );
283285
if ( ! $bytes_written ) {
284286
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." );
288352
}
289353

290354
/**

0 commit comments

Comments
 (0)