Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/WP_CLI/JsonManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public function removeSubNode($mainNode, $name, $caseInsensitive = false) // WP_
if (preg_match_all($regex, $value, $nameMatches)) {
// Just zap them all individually in a case sensitive manner.
foreach ( $nameMatches['name'] as $nameMatch ) {
$this->removeSubNode($mainNode, $nameMatch);
$this->removeSubNode($mainNode, str_replace('\\/', '/', $nameMatch)); // Unescape JSON slashes so the key matches the decoded data.
}
}

Expand Down Expand Up @@ -386,9 +386,10 @@ public function removeSubNode($mainNode, $name, $caseInsensitive = false) // WP_

// try and find a match for the subkey
$childrenClean = $children;
if ($this->pregMatch('{"'.preg_quote($name).'"\s*:}i', $children)) {
$nameRegex = str_replace('/', '\\\\?/', preg_quote($name)); // WP_CLI: Also match a JSON-escaped slash (`\/`) in the key.
if ($this->pregMatch('{"'.$nameRegex.'"\s*:}i', $children)) {
// find best match for the value of "name"
if (preg_match_all('{'.self::$DEFINES.'"'.preg_quote($name).'"\s*:\s*(?:(?&json))}x', $children, $matches)) {
if (preg_match_all('{'.self::$DEFINES.'"'.$nameRegex.'"\s*:\s*(?:(?&json))}x', $children, $matches)) {
$bestMatch = '';
foreach ($matches[0] as $match) {
if (strlen($bestMatch) < strlen($match)) {
Expand Down
30 changes: 30 additions & 0 deletions tests/phpunit/JsonManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,36 @@ public function testRemoveSubNodeFromRequire()
', $manipulator->getContents());
}

/**
* @dataProvider removeSubNodeEscapedKeyProvider
*/
#[DataProvider( 'removeSubNodeEscapedKeyProvider' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testRemoveSubNodeCaseInsensitiveWithEscapedSlash($name)
{
$manipulator = new JsonManipulator('{
"require": {
"wp-cli\/foo": "^1.0",
"wp-cli/bar": "^1.0"
}
}');

$this->assertTrue($manipulator->removeSubNode('require', $name, true));
$this->assertEquals('{
"require": {
"wp-cli/bar": "^1.0"
}
}
', $manipulator->getContents());
}

public static function removeSubNodeEscapedKeyProvider()
{
return array(
'same case' => array('wp-cli/foo'),
'different case' => array('WP-CLI/Foo'),
);
}

public function testAddSubNodeInRequire()
{
$manipulator = new JsonManipulator('{
Expand Down