Skip to content

Commit 04a944e

Browse files
swissspidyclaude
andcommitted
Keep the original key when a --recurse-keys regex replacement fails
On a PCRE error preg_replace() returns null, which was used as the new array key and silently turned the key into an empty string. Keep the original key in that case, matching the warning that is already shown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent b7d9e53 commit 04a944e

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎features/search-replace.feature‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,3 +1976,30 @@ Feature: Do global search/replace
19761976
"""
19771977
"new":"v2"
19781978
"""
1979+
1980+
@require-mysql
1981+
Scenario: Keep the original key when a regex replacement fails with --recurse-keys
1982+
Given a WP install
1983+
And a setup-recurse-keys-pcre.php file:
1984+
"""
1985+
<?php
1986+
update_option( 'recurse_keys_pcre', array( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!' => 'keep!' ) );
1987+
"""
1988+
And a check-recurse-keys-pcre.php file:
1989+
"""
1990+
<?php
1991+
echo json_encode( get_option( 'recurse_keys_pcre' ) );
1992+
"""
1993+
And I run `wp eval-file setup-recurse-keys-pcre.php`
1994+
1995+
When I try `wp search-replace '^(a+\s?)+$' 'x' wp_options --include-columns=option_value --regex --recurse-keys`
1996+
Then STDERR should contain:
1997+
"""
1998+
The provided regular expression threw a PCRE error
1999+
"""
2000+
2001+
When I run `wp eval-file check-recurse-keys-pcre.php`
2002+
Then STDOUT should be:
2003+
"""
2004+
{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!":"keep!"}
2005+
"""

‎src/WP_CLI/SearchReplacer.php‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,24 @@ private function replace_string( $data ) {
290290
return $data;
291291
}
292292

293+
/**
294+
* Replace occurrences of the search string within an array key or property name.
295+
*
296+
* Integer keys are returned unchanged. If the replacement fails (for
297+
* example a PCRE error in regex mode), the original key is kept instead of
298+
* turning it into an empty string.
299+
*
300+
* @param int|string $key The key or property name.
301+
* @return int|string The replaced key, or the original key.
302+
*/
303+
private function replace_key( $key ) {
304+
if ( ! is_string( $key ) ) {
305+
return $key;
306+
}
307+
$new_key = $this->replace_string( $key );
308+
return is_string( $new_key ) ? $new_key : $key;
309+
}
310+
293311
/**
294312
* Recurse into an array, replacing string keys as well as values.
295313
*
@@ -306,7 +324,7 @@ private function replace_string( $data ) {
306324
private function replace_array_keys( $data, $recursion_level, $visited_data ) {
307325
$renames = array();
308326
foreach ( array_keys( $data ) as $key ) {
309-
$renames[ $key ] = is_string( $key ) ? $this->replace_string( $key ) : $key;
327+
$renames[ $key ] = $this->replace_key( $key );
310328
}
311329
if ( $this->has_destination_collision( $renames, false ) ) {
312330
\WP_CLI::warning( 'Skipping key replacement for an array: two keys would map onto the same key and a value would be lost.' );
@@ -345,7 +363,7 @@ private function replace_array_keys( $data, $recursion_level, $visited_data ) {
345363
private function replace_object_property_names( $data, $is_array_access, $recursion_level, $visited_data ) {
346364
$renames = array();
347365
foreach ( $data as $key => $value ) {
348-
$renames[ $key ] = is_string( $key ) ? $this->replace_string( $key ) : $key;
366+
$renames[ $key ] = $this->replace_key( $key );
349367
}
350368

351369
$case_insensitive = false;

0 commit comments

Comments
 (0)