diff --git a/src/WPConfigTransformer.php b/src/WPConfigTransformer.php index 7957375..d6ba7b0 100644 --- a/src/WPConfigTransformer.php +++ b/src/WPConfigTransformer.php @@ -226,8 +226,19 @@ public function remove( $type, $name ) { return false; } - $pattern = sprintf( '/(?<=^|;|<\?php\s|<\?\s)%s\s*(\S|$)/m', preg_quote( $this->wp_configs[ $type ][ $name ]['src'], '/' ) ); - $contents = preg_replace( $pattern, '$1', $this->wp_config_src ); + if ( 'constant' === $type ) { + $pattern = sprintf( + "/\bdefine\s*\(\s*['\"]%s['\"]\s*,\s*(('[^']*'|\"[^\"]*\")|\s*(?:[\s\S]*?))\s*\)\s*;\s*/mi", + preg_quote( $name, '/' ) + ); + } else { + $pattern = sprintf( + '/^\s*\$%s\s*=\s*[\s\S]*?;\s*$/mi', + preg_quote( $name, '/' ) + ); + } + + $contents = preg_replace( $pattern, '', $this->wp_config_src ); return $this->save( $contents ); } diff --git a/tests/AddRemoveTest.php b/tests/AddRemoveTest.php index db1fcd8..3b3e6f1 100644 --- a/tests/AddRemoveTest.php +++ b/tests/AddRemoveTest.php @@ -142,6 +142,25 @@ public function testRemoveStringVariables() { } } + public function testRemoveConstantWithConcatenation() { + // Set up the initial state by defining a constant with concatenation + $name = 'TEST_USER_PATH'; + $value = "'/var/www/' . get_current_user()"; + $this->assertTrue( self::$config_transformer->add( 'constant', $name, $value, array( 'raw' => true ) ), "Adding constant {$name}" ); + + // Define another constant to check if it remains untouched + $second_name = 'TEST_THIS_AND'; + $second_value = "md5('that')"; + $this->assertTrue( self::$config_transformer->add( 'constant', $second_name, $second_value, array( 'raw' => true ) ), "Adding constant {$second_name}" ); + + // Remove the first constant and check the result + $this->assertTrue( self::$config_transformer->remove( 'constant', $name ), "Removing constant {$name}" ); + $this->assertFalse( self::$config_transformer->exists( 'constant', $name ), "Check {$name} does not exist" ); + + // Ensure the second constant is still present after the removal + $this->assertTrue( self::$config_transformer->exists( 'constant', $second_name ), "Check {$second_name} still exists" ); + } + public function testAddConstantNoPlacementAnchor() { $this->expectException( Exception::class ); $this->expectExceptionMessage( 'Unable to locate placement anchor.' );