wp-config-transformer icon indicating copy to clipboard operation
wp-config-transformer copied to clipboard

Deletion of constants deletes more lines than expected

Open shreya0204 opened this issue 8 months ago • 4 comments

Fixes: #50 and wp-cli/config-command#101

This pull request addresses the bug where using wp config delete to remove a constant from wp-config.php that includes concatenated functions or complex strings results in the incorrect deletion of more than just the targeted constant.

Current Behavior

Currently, when deleting a constant that includes concatenated functions or special characters, the regex used in the deletion function does not properly isolate the constant. This results in deleting additional lines or even other constants. For example:

define( 'WP_DEBUG', false );
define( 'USER_PATH', '/var/www/' . get_current_user() );
define( 'THIS_AND', md5( 'that' ) );
if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
	$_SERVER['HTTPS'] = 'on';
}
define( 'DISABLE_WP_CRON', true );

Running wp config delete USER_PATH would improperly affect adjacent lines.

Steps to Replicate

  1. Have a wp-config.php file with a constant definition concatenating a function.
  2. Run wp config delete USER_PATH.

Expected Outcome

Only the line defining USER_PATH should be removed, leaving all other settings and definitions intact.

Issue Details

The issue stemmed from the regex pattern used in the removal function, which failed to correctly identify the boundaries of the constant definition when it involved concatenated functions or complex string values.

Proposed Solution

This PR introduces a revised regex pattern that more accurately matches and isolates define statements, even when they contain complex string values or are among multiple statements on a single line. The new regex handles variations in whitespace and formatting, ensuring that only the specific constant is removed.

shreya0204 avatar Jun 20 '24 08:06 shreya0204