Skip to content

Commit e9440f3

Browse files
committed
feat: add opt-in --recurse-keys flag for serialized keys
Serialized array keys and object property names now pass through the same replacement branch as values when --recurse-keys is set. Default behavior is unchanged.
1 parent 2a1ff61 commit e9440f3

3 files changed

Lines changed: 212 additions & 37 deletions

File tree

‎features/search-replace.feature‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,3 +1592,125 @@ Feature: Do global search/replace
15921592
"""
15931593
Table is read-only
15941594
"""
1595+
1596+
@require-mysql
1597+
Scenario: Serialized array keys are left alone by default
1598+
Given a WP install
1599+
And a setup-recurse-keys.php file:
1600+
"""
1601+
<?php
1602+
update_option( 'recurse_keys_test', array( 'https://old.example.com/key' => 'https://old.example.com/value', 'nested' => array( 'https://old.example.com/inner' => 'plain' ) ) );
1603+
"""
1604+
And a check-recurse-keys.php file:
1605+
"""
1606+
<?php
1607+
echo json_encode( get_option( 'recurse_keys_test' ), JSON_UNESCAPED_SLASHES );
1608+
"""
1609+
And I run `wp eval-file setup-recurse-keys.php`
1610+
1611+
When I run `wp search-replace 'https://old.example.com' 'https://new.example.com'`
1612+
Then STDOUT should contain:
1613+
"""
1614+
Success:
1615+
"""
1616+
1617+
When I run `wp eval-file check-recurse-keys.php`
1618+
Then STDOUT should contain:
1619+
"""
1620+
https://old.example.com/key
1621+
"""
1622+
And STDOUT should contain:
1623+
"""
1624+
https://new.example.com/value
1625+
"""
1626+
And STDOUT should not contain:
1627+
"""
1628+
https://new.example.com/key
1629+
"""
1630+
1631+
@require-mysql
1632+
Scenario: Search/replace serialized array keys with --recurse-keys
1633+
Given a WP install
1634+
And a setup-recurse-keys.php file:
1635+
"""
1636+
<?php
1637+
update_option( 'recurse_keys_test', array( 'https://old.example.com/key' => 'https://old.example.com/value', 'nested' => array( 'https://old.example.com/inner' => 'plain' ) ) );
1638+
"""
1639+
And a check-recurse-keys.php file:
1640+
"""
1641+
<?php
1642+
echo json_encode( get_option( 'recurse_keys_test' ), JSON_UNESCAPED_SLASHES );
1643+
"""
1644+
And I run `wp eval-file setup-recurse-keys.php`
1645+
1646+
When I run `wp search-replace 'https://old.example.com' 'https://new.example.com' --recurse-keys`
1647+
Then STDOUT should contain:
1648+
"""
1649+
Success:
1650+
"""
1651+
1652+
When I run `wp eval-file check-recurse-keys.php`
1653+
Then STDOUT should contain:
1654+
"""
1655+
https://new.example.com/key
1656+
"""
1657+
And STDOUT should contain:
1658+
"""
1659+
https://new.example.com/value
1660+
"""
1661+
And STDOUT should contain:
1662+
"""
1663+
https://new.example.com/inner
1664+
"""
1665+
And STDOUT should not contain:
1666+
"""
1667+
old.example.com
1668+
"""
1669+
1670+
@require-mysql
1671+
Scenario: Search/replace object property names with --recurse-keys
1672+
Given a WP install
1673+
And a setup-recurse-keys-object.php file:
1674+
"""
1675+
<?php
1676+
update_option( 'recurse_keys_object_test', (object) array( 'https://old.example.com/prop' => 'https://old.example.com/value' ) );
1677+
"""
1678+
And a check-recurse-keys-object.php file:
1679+
"""
1680+
<?php
1681+
echo json_encode( get_option( 'recurse_keys_object_test' ), JSON_UNESCAPED_SLASHES );
1682+
"""
1683+
And I run `wp eval-file setup-recurse-keys-object.php`
1684+
1685+
When I run `wp search-replace 'https://old.example.com' 'https://new.example.com'`
1686+
Then STDOUT should contain:
1687+
"""
1688+
Success:
1689+
"""
1690+
1691+
When I run `wp eval-file check-recurse-keys-object.php`
1692+
Then STDOUT should contain:
1693+
"""
1694+
https://old.example.com/prop
1695+
"""
1696+
And STDOUT should contain:
1697+
"""
1698+
https://new.example.com/value
1699+
"""
1700+
1701+
When I run `wp eval-file setup-recurse-keys-object.php`
1702+
And I run `wp search-replace 'https://old.example.com' 'https://new.example.com' --recurse-keys`
1703+
Then STDOUT should contain:
1704+
"""
1705+
Success:
1706+
"""
1707+
1708+
When I run `wp eval-file check-recurse-keys-object.php`
1709+
Then STDOUT should contain:
1710+
"""
1711+
https://new.example.com/prop
1712+
"""
1713+
And STDOUT should not contain:
1714+
"""
1715+
old.example.com
1716+
"""

‎src/Search_Replace_Command.php‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class Search_Replace_Command extends WP_CLI_Command {
3737
*/
3838
private $recurse_objects;
3939

40+
/**
41+
* @var bool
42+
*/
43+
private $recurse_keys;
44+
4045
/**
4146
* @var bool
4247
*/
@@ -215,6 +220,10 @@ class Search_Replace_Command extends WP_CLI_Command {
215220
* : Enable recursing into objects to replace strings. Defaults to true;
216221
* pass --no-recurse-objects to disable.
217222
*
223+
* [--recurse-keys]
224+
* : Enable recursing into array keys and object properties to replace strings.
225+
* Defaults to false; pass --recurse-keys to enable.
226+
*
218227
* [--verbose]
219228
* : Prints rows to the console as they're updated.
220229
*
@@ -291,7 +300,7 @@ class Search_Replace_Command extends WP_CLI_Command {
291300
* fi
292301
*
293302
* @param array<string> $args Positional arguments.
294-
* @param array{'old'?: string, 'new'?: string, 'dry-run'?: bool, 'network'?: bool, 'all-tables-with-prefix'?: bool, 'all-tables'?: bool, 'export'?: string, 'export_insert_size'?: string, 'skip-tables'?: string, 'skip-columns'?: string, 'include-columns'?: string, 'precise'?: bool, 'recurse-objects'?: bool, 'verbose'?: bool, 'regex'?: bool, 'regex-flags'?: string, 'regex-delimiter'?: string, 'regex-limit'?: string, 'format': string, 'report'?: bool, 'report-changed-only'?: bool, 'log'?: string, 'before_context'?: string, 'after_context'?: string} $assoc_args Associative arguments.
303+
* @param array{'old'?: string, 'new'?: string, 'dry-run'?: bool, 'network'?: bool, 'all-tables-with-prefix'?: bool, 'all-tables'?: bool, 'export'?: string, 'export_insert_size'?: string, 'skip-tables'?: string, 'skip-columns'?: string, 'include-columns'?: string, 'precise'?: bool, 'recurse-objects'?: bool, 'recurse-keys'?: bool, 'verbose'?: bool, 'regex'?: bool, 'regex-flags'?: string, 'regex-delimiter'?: string, 'regex-limit'?: string, 'format': string, 'report'?: bool, 'report-changed-only'?: bool, 'log'?: string, 'before_context'?: string, 'after_context'?: string} $assoc_args Associative arguments.
295304
*/
296305
public function __invoke( $args, $assoc_args ) {
297306
global $wpdb;
@@ -337,6 +346,7 @@ public function __invoke( $args, $assoc_args ) {
337346
$this->dry_run = Utils\get_flag_value( $assoc_args, 'dry-run', false );
338347
$php_only = Utils\get_flag_value( $assoc_args, 'precise', false );
339348
$this->recurse_objects = Utils\get_flag_value( $assoc_args, 'recurse-objects', true );
349+
$this->recurse_keys = Utils\get_flag_value( $assoc_args, 'recurse-keys', false );
340350
$this->verbose = Utils\get_flag_value( $assoc_args, 'verbose', false );
341351
$this->format = Utils\get_flag_value( $assoc_args, 'format' );
342352
$this->regex = Utils\get_flag_value( $assoc_args, 'regex', false );
@@ -642,7 +652,7 @@ private function php_export_table( $table, $old, $new ) {
642652
'chunk_size' => $chunk_size,
643653
);
644654

645-
$replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, false, $this->regex_limit );
655+
$replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, false, $this->regex_limit, $this->recurse_keys );
646656
$col_counts = array_fill_keys( $all_columns, 0 );
647657
if ( $this->verbose && 'table' === $this->format ) {
648658
$this->start_time = microtime( true );
@@ -747,7 +757,7 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
747757
global $wpdb;
748758

749759
$count = 0;
750-
$replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, null !== $this->log_handle, $this->regex_limit );
760+
$replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, null !== $this->log_handle, $this->regex_limit, $this->recurse_keys );
751761

752762
$table_sql = self::esc_sql_ident( $table );
753763
$col_sql = self::esc_sql_ident( $col );

‎src/WP_CLI/SearchReplacer.php‎

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class SearchReplacer {
3232
*/
3333
private $recurse_objects;
3434

35+
/**
36+
* @var bool
37+
*/
38+
private $recurse_keys;
39+
3540
/**
3641
* @var bool
3742
*/
@@ -81,11 +86,13 @@ class SearchReplacer {
8186
* @param string $regex_delimiter Delimiter for regular expression.
8287
* @param bool $logging Whether logging.
8388
* @param integer $regex_limit The maximum possible replacements for each pattern in each subject string.
89+
* @param bool $recurse_keys Should array keys and object property names be recursively replaced?
8490
*/
85-
public function __construct( $from, $to, $recurse_objects = false, $regex = false, $regex_flags = '', $regex_delimiter = '/', $logging = false, $regex_limit = -1 ) {
91+
public function __construct( $from, $to, $recurse_objects = false, $regex = false, $regex_flags = '', $regex_delimiter = '/', $logging = false, $regex_limit = -1, $recurse_keys = false ) {
8692
$this->from = $from;
8793
$this->to = $to;
8894
$this->recurse_objects = $recurse_objects;
95+
$this->recurse_keys = $recurse_keys;
8996
$this->regex = $regex;
9097
$this->regex_flags = $regex_flags;
9198
$this->regex_delimiter = $regex_delimiter;
@@ -180,10 +187,17 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
180187
if ( false !== $unserialized ) {
181188
$data = $this->run_recursively( $unserialized, true, $recursion_level + 1 );
182189
} elseif ( is_array( $data ) ) {
183-
$keys = array_keys( $data );
190+
$keys = array_keys( $data );
191+
$replaced = array();
184192
foreach ( $keys as $key ) {
185-
$data[ $key ] = $this->run_recursively( $data[ $key ], false, $recursion_level + 1, $visited_data );
193+
$value = $this->run_recursively( $data[ $key ], false, $recursion_level + 1, $visited_data );
194+
$new_key = $key;
195+
if ( $this->recurse_keys && is_string( $key ) ) {
196+
$new_key = $this->replace_string( $key );
197+
}
198+
$replaced[ $new_key ] = $value;
186199
}
200+
$data = $replaced;
187201
} elseif ( $this->recurse_objects && ( is_object( $data ) || $data instanceof \__PHP_Incomplete_Class ) ) {
188202
if ( $data instanceof \__PHP_Incomplete_Class ) {
189203
$array = new ArrayObject( $data );
@@ -196,12 +210,26 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
196210
} else {
197211
try {
198212
$is_array_access = $data instanceof \ArrayAccess;
213+
$properties = array();
199214
foreach ( $data as $key => $value ) {
200-
$value = $this->run_recursively( $value, false, $recursion_level + 1, $visited_data );
215+
$properties[ $key ] = $value;
216+
}
217+
foreach ( $properties as $key => $value ) {
218+
$value = $this->run_recursively( $value, false, $recursion_level + 1, $visited_data );
219+
$new_key = $key;
220+
if ( $this->recurse_keys && is_string( $key ) ) {
221+
$new_key = $this->replace_string( $key );
222+
}
201223
if ( $is_array_access ) {
202-
$data[ $key ] = $value;
224+
if ( $new_key !== $key ) {
225+
unset( $data[ $key ] );
226+
}
227+
$data[ $new_key ] = $value;
203228
} else {
204-
$data->$key = $value;
229+
if ( $new_key !== $key ) {
230+
unset( $data->$key );
231+
}
232+
$data->$new_key = $value;
205233
}
206234
}
207235
} catch ( \Error $exception ) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
@@ -220,34 +248,7 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
220248
}
221249
}
222250
} elseif ( is_string( $data ) ) {
223-
if ( $this->logging ) {
224-
$old_data = $data;
225-
}
226-
if ( $this->regex ) {
227-
$search_regex = $this->regex_delimiter;
228-
$search_regex .= $this->from;
229-
$search_regex .= $this->regex_delimiter;
230-
$search_regex .= $this->regex_flags;
231-
232-
$result = preg_replace( $search_regex, $this->to, $data, $this->regex_limit );
233-
if ( null === $result || PREG_NO_ERROR !== preg_last_error() ) {
234-
\WP_CLI::warning(
235-
sprintf(
236-
'The provided regular expression threw a PCRE error - %s',
237-
$this->preg_error_message( $result )
238-
)
239-
);
240-
}
241-
$data = $result;
242-
} else {
243-
$data = str_replace( $this->from, $this->to, $data );
244-
if ( $this->from_json !== $this->from ) {
245-
$data = str_replace( $this->from_json, $this->to_json, $data );
246-
}
247-
}
248-
if ( $this->logging && $old_data !== $data ) {
249-
$this->log_data[] = $old_data;
250-
}
251+
$data = $this->replace_string( $data );
251252
}
252253

253254
if ( $serialised ) {
@@ -260,6 +261,48 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
260261
return $data;
261262
}
262263

264+
/**
265+
* Replace occurrences of the search string within a single string.
266+
*
267+
* Used for values as well as array keys and object property names when
268+
* key recursion is enabled.
269+
*
270+
* @param string $data The string to operate on.
271+
* @return string The string with all replacements made as needed.
272+
*/
273+
private function replace_string( $data ) {
274+
if ( $this->logging ) {
275+
$old_data = $data;
276+
}
277+
if ( $this->regex ) {
278+
$search_regex = $this->regex_delimiter;
279+
$search_regex .= $this->from;
280+
$search_regex .= $this->regex_delimiter;
281+
$search_regex .= $this->regex_flags;
282+
283+
$result = preg_replace( $search_regex, $this->to, $data, $this->regex_limit );
284+
if ( null === $result || PREG_NO_ERROR !== preg_last_error() ) {
285+
\WP_CLI::warning(
286+
sprintf(
287+
'The provided regular expression threw a PCRE error - %s',
288+
$this->preg_error_message( $result )
289+
)
290+
);
291+
}
292+
$data = $result;
293+
} else {
294+
$data = str_replace( $this->from, $this->to, $data );
295+
if ( $this->from_json !== $this->from ) {
296+
$data = str_replace( $this->from_json, $this->to_json, $data );
297+
}
298+
}
299+
if ( $this->logging && $old_data !== $data ) {
300+
$this->log_data[] = $old_data;
301+
}
302+
303+
return $data;
304+
}
305+
263306
/**
264307
* Gets existing data saved for this run when logging.
265308
* @return string[] Array of data strings, prior to replacements.

0 commit comments

Comments
 (0)