Open todeveni opened 4 years ago
I’ve been following this thread and experiencing the same issue with the wp config delete
command removing more than just the targeted constant, especially when the definition includes concatenated functions. Here's a bit more about what I've tried and discovered:
Issue Recap:
When using wp config delete
to remove constants like:
define('USER_PATH', '/var/www/' . get_current_user());
the command ends up deleting additional lines, not just the USER_PATH
line.
Deeper Dive:
I noticed that the regex in our remove()
function within the WPConfigTransformer
class function was too broad, affecting more than the intended line. The regex seemed to fail in properly isolating the constant definition when it involved function concatenations.
Approach Tried:
remove()
function and implemented direct file manipulation based on regex matching for both constants and variables:if ('constant' === $type) {
$pattern = "/define\s*\(\s*['\"]{$name}['\"]\s*,/";
} else {
$pattern = '/^\\$' . $name . "\s*=\s*['\"][^'\"\;]*['\"]\s*;/";
}
// I iterated through each line to find and remove the exact match.
This approach allowed me to precisely target and delete only the intended lines. After making these adjustments, all my test cases ran perfectly, confirming that the specific constant or variable was successfully removed without impacting surrounding lines.
Suggested Next Steps:
Given the effectiveness of directly manipulating file content with tailored regex patterns, it might be worthwhile for us to revisit how the remove()
function handles pattern matching. Refining this function could ensure better accuracy and prevent unintended deletions in future releases.
I’m very open to feedback and would love to collaborate on fixing this issue.
I can also reproduce the issue. I tested with following feature test.
@require-mysql
Scenario: Delete constant with concatenated strings
Given an empty directory
And WP files
And a wp-config-extra.php file:
"""
define( 'USER_PATH', '/var/www/' . get_current_user() );
"""
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check --extra-php < wp-config-extra.php`
Then the wp-config.php file should contain:
"""
'USER_PATH',
"""
When I run `wp config delete USER_PATH`
Then the wp-config.php file should not contain:
"""
'USER_PATH'
"""
But here in my case, rather than deleting unrelated changes, command is not deleting anything.
Bug Report
Describe the current, buggy behavior
Deleting a constant with concatenated function from
wp-config.php
withwp config delete
deletes more than just that constant.Describe how other contributors can replicate this bug
wp config delete USER_PATH
Describe what you would expect as the correct outcome
wp-config.php
with justUSER_PATH
constant definition removed.Let us know what environment you are running this on